本文主要是介绍Android 自定义View控件,实现跟随手指触摸移动的小球,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android UI组件是通过继承View类,然后绘制内容,比如ImageView,TextView等组件都是继承View类。
当Android系统提供的组件功能不能满足需求时,可以通过继承View类重写一个或多个方法,派生自定义的组件,View类常用重写方法:
1.构造器:View子类最基本的重写方法
protected voidonDraw(Canvas canvas)
public booleanonTouchEvent(MotionEvent event)
public boolean onTrackballEvent(MotionEvent event)
public boolean onKeyDown(int keyCode, KeyEvent event)public boolean onKeyUp(int keyCode, KeyEvent event)protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)//检测View组件及子组件大小protected void onSizeChanged(int w, int h, int oldw, int oldh)protected boolean onSetAlpha(int alpha)protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)public void onWindowFocusChanged(boolean hasWindowFocus)protected void onFinishInflate()//回调方法,当应用从xml文件加载构建界面之后此方法会被回调
定义DrawView 实现跟随手指移动的小球 DrawView.java
package shortcut.song.com.myapplication;import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View;/** * Created by Administrator on 2017/1/6 0006. */ public class DrawView extends View {public float currentX = 80;public float currentY = 60;public DrawView(Context context){super(context);}public DrawView(Context context, AttributeSet attributeSet){super(context, attributeSet);}@Override protected void onDraw(Canvas canvas) {Paint p = new Paint();p.setColor(Color.RED);canvas.drawCircle(currentX, currentY, 10, p);}@Override public boolean onTouchEvent(MotionEvent event) {this.currentX = event.getX(); //触摸座标X this.currentY = event.getY(); //触摸座标Y invalidate();//重绘组件 return true;//返回true:事件已处理,此处必须返回true,否则小球移动不了。 } }
//Layout文件activity_draw_view.xml
//通过代码编程实现时没用到此文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_draw_view" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="shortcut.song.com.myapplication.DrawViewActivity"><!-- 自定义的DrawView组件 --><shortcut.song.com.myapplication.DrawView android:id="@+id/drawview" android:layout_width="wrap_content" android:layout_height="wrap_content"android:background="#0ff0ff"/></RelativeLayout>
DrawViewActivity.java
package shortcut.song.com.myapplication;import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout;public class DrawViewActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//通过XML布局实现 //setContentView(R.layout.activity_draw_view); //final DrawView mDrawView = (DrawView)findViewById(R.id.drawview); //通过代码编程实现 LinearLayout main = new LinearLayout(this);main.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));main.setOrientation(LinearLayout.HORIZONTAL);main.setGravity(Gravity.CENTER);DrawView mDrawView = new DrawView(this);mDrawView.setBackgroundColor(Color.BLUE);main.addView(mDrawView);setContentView(main);//设置Aictivity 显示View } }
这篇关于Android 自定义View控件,实现跟随手指触摸移动的小球的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!