繪制當(dāng)前觸控操作,自定義View(畫隨指動)

實現(xiàn)手指點擊和觸摸移動時,圖片跟隨移動,想象成簡陋的坦克大戰(zhàn)即可

直接上代碼:
第一步:先自定義一個View類,用作繪制View

public class MoveView extends View {

    private Bitmap bitmap;
    private float planeX=1f;
    private float planeY=1f;
    public Paint paint = new Paint();
    public MoveView(Context context) {
        super(context);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        paint.setDither(true);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tanke);//通過使用bitmapfactory的decoderesource來獲得bitmap
    }

    public MoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
         //canvas.drawCircle(planeX, planeY, 20, paint);//也可以直接繪制一個圓
        canvas.drawBitmap(bitmap,planeX-bitmap.getWidth()/2,planeY-bitmap.getHeight()/2,paint);
    }
//接收位置信息
    public void setPlane(float x,float y) {
        planeX=x;
        planeY=y;

    }
}

第二步:寫布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv_info"
            android:text="坐標(biāo):xxx,xxx"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <com.example.lp.mydraw.MyView.MoveView
            android:id="@+id/mv_move"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </RelativeLayout>



</LinearLayout>
image.png

第三步:編寫Activity,重寫onTouchEvent來執(zhí)行觸摸操作,并將觸摸操作傳遞到自定義的view中,繪制圖形。

public class MainActivity extends AppCompatActivity {
    private MoveView moveView;
    private TextView locationInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        moveView=findViewById(R.id.mv_move);
        locationInfo=findViewById(R.id.tv_info);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//設(shè)置全屏

    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        locationInfo.setText("當(dāng)前坐標(biāo)\n"+event.getX()+" , "+event.getY());
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                moveView.setPlane(event.getX(),event.getY());
                moveView.invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                moveView.setPlane(event.getX(),event.getY());
                moveView.invalidate();
                break;
        }

        return super.onTouchEvent(event);

    }
} 

資源圖片:tanke.png


tanke.png

大功告成。
demo連接:https://github.com/CodeLpea/PracticceOfAndorid.git

image.png

謝謝大家。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容