• Android canvas+paint绘制一个可以指定位置移动的小球(含触屏响应)


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <EditText
            android:id="@+id/txt_1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/but_1"
            android:text="确认"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
            <com.android.canvstest.TestView
                android:layout_height="wrap_content"
                android:id="@+id/textview1"
                android:layout_width="wrap_content"/>
    </LinearLayout>
    package com.android.canvstest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final TestView testView = findViewById(R.id.textview1);
            Button button = findViewById(R.id.but_1);
            final EditText editText = findViewById(R.id.txt_1);
            testView.setXY(100,100);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int x=100;
                    x = Integer.parseInt(editText.getText().toString());
                    testView.setXY(x,100);
                    testView.invalidate();
                }
            });
        }
    
    }

    自定义TextView

    package com.android.canvstest;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.View;
    
    import androidx.annotation.Nullable;
    
    public class TestView extends View {
    
        int x,y;
    
        public TestView(Context context, @Nullable AttributeSet attrs) {    //必须使用AttributeSet接口做对象的参数
            super(context, attrs);
        }
    
    
        void setXY(int x1,int y1){
            x=x1;
            y=y1;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.BLUE);
    
            Paint paint=new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.GREEN);
            paint.setStyle(Paint.Style.FILL);
    
            canvas.drawCircle(x,y,50,paint);
            paint.setColor(Color.RED);
            canvas.drawCircle(x,y,10,paint);
    
        }
    }

     后面我又写了一下触屏移动操作(只需要改动MainActivity):

    package com.android.canvstest;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;


    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TestView testView = findViewById(R.id.textview1);
    Button button = findViewById(R.id.but_1);
    final EditText editText = findViewById(R.id.txt_1);
    testView.setXY(100, 100);

    testView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    int x,y;
    x = (int) event.getX();
    y = (int) event.getY();
    if (event.getAction() == MotionEvent.ACTION_DOWN){
    testView.setXY(x,y);
    testView.invalidate();
    return true;
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE){
    testView.setXY(x,y);
    testView.invalidate();
    return true;
    }
    return testView.onTouchEvent(event);
    }
    });
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    int x=100;
    x = Integer.parseInt(editText.getText().toString());
    testView.setXY(x,100);
    testView.invalidate();
    }
    });
    }
    }
  • 相关阅读:
    JStorm开发经验+运维经验总结
    Storm-166:Nimbus HA solution based on Zookeeper
    Storm实战集锦
    JStorm之Nimbus简介
    BF算法 + KMP算法
    分布式消息系统:Kafka
    分布式服务框架:Zookeeper简介
    修改JSONArray里所有key的值
    JQuery中$.ajax()方法参数详解
    java利用16进制来辨别png格式的图片
  • 原文地址:https://www.cnblogs.com/xiaowangdatie/p/14126675.html
Copyright © 2020-2023  润新知