瘋狂 Android 講義, 4/e – 自定義UI元件(使用程式語法新增元件)/Android View的基本事件函數介紹 P69~P71
瘋狂 Android 講義, 4/e – 自定義UI元件(使用程式語法新增元件)/Android View的基本事件函數介紹 P69~P71
資料來源:
https://github.com/daichangya/book/tree/master/android
https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch
Code
package org.crazyit.ui; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Description:<br> * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br> * Copyright (C), 2001-2020, Yeeku.H.Lee<br> * This program is protected by copyright laws.<br> * Program Name:<br> * Date:<br> * @author Yeeku.H.Lee kongyeeku@163.com<br> * @version 1.0 */ public class DrawView extends View { private float currentX = 40f; private float currentY = 50f; // 定义并创建画笔 private Paint p = new Paint(); public DrawView(Context context) { super(context); } public DrawView(Context context, AttributeSet set) { super(context, set); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); // 设置画笔的颜色 p.setColor(Color.RED); // 绘制一个小圆(作为小球) canvas.drawCircle(currentX, currentY, 15F, p); } // 为该组件的触碰事件重写事件处理方法 @Override public boolean onTouchEvent(MotionEvent event) { // 修改currentX、currentY两个属性 currentX = event.getX(); currentY = event.getY(); // 通知当前组件重绘自己 invalidate(); // 返回true表明该处理方法已经处理该事件 return true; } }
package org.crazyit.ui; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; /** * Description:<br> * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br> * Copyright (C), 2001-2020, Yeeku.H.Lee<br> * This program is protected by copyright laws.<br> * Program Name:<br> * Date:<br> * @author Yeeku.H.Lee kongyeeku@163.com<br> * @version 1.0 */ public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取布局文件中的LinearLayout容器 LinearLayout root = findViewById(R.id.root); // 创建DrawView组件 DrawView draw = new DrawView(this); // 设置自定义组件的最小宽度、高度 draw.setMinimumWidth(300); draw.setMinimumHeight(500); root.addView(draw); } }
<?xml version="1.0" encoding="utf-8"?> <!-- 定义一个线性布局容器 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:id="@+id/root" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout>