zoukankan      html  css  js  c++  java
  • Android-自定义View

    自定义一个跟随手指的小球

    DrawView.java文件

    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;
    
    public class DrawView extends View{
     
        
        public float currentX=40;
        public float currentY=50;
        
        Paint p=new Paint();//定义画笔
        
        public DrawView(Context context) {
            super(context);
            
        }
        //构造器一定要有否则报错
         public DrawView(Context context, AttributeSet attrs){  
                super(context, attrs);  
            }  
        
        @Override
        protected void onDraw(Canvas canvas) {
            
            super.onDraw(canvas);
            p.setColor(Color.RED);
            canvas.drawCircle(currentX, currentY, 15, p);//画圆形
            
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            
            currentX=event.getX();
            currentY=event.getY();
            
            //通知组件重绘制
            this.invalidate(); 
            return true;
        }
    
    
        
    }

    layout.xml加入

     <com.layer.study3027.DrawView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            
            
            
            /> 
  • 相关阅读:
    课程个人总结
    构建之法阅读笔记06
    构建之法读后感5
    第五周进度条
    提高自身能力
    活动图与状态机图
    对分析业务模型----类图的学习与认识
    需求分析工作的基本道理
    问题账户需求分析
    2016秋季个人阅读计划
  • 原文地址:https://www.cnblogs.com/leeAsia/p/3333989.html
Copyright © 2011-2022 走看看