• 自定义View,随着手指运动的小球


    MainActivity.java

    package com.kale.drawview;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    /*		RelativeLayout root  = (RelativeLayout)findViewById(R.id.root_relativeLayout_id);
    		
    		final DrawView drawView = new DrawView(this);
    		//設置組件的最大寬度
    		drawView.setMinimumHeight(300);
    		drawView.setMinimumWidth(500);
    		root.addView(drawView);*/
    	}
    }
    


    DrawView.java 自定义的view

    package com.kale.drawview;
    
    
    import android.annotation.SuppressLint;
    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 = 60;
    	public float currentY = 60;
    	
    	//定义,创建画笔
    	Paint paint = 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);
    		//设置画笔颜色
    		paint.setColor(Color.RED);
    		//绘制一个小圆
    		canvas.drawCircle(currentX, currentY, 50, paint);
    	}
    	
    	// 为该组件的触碰时间2重写处理的方法
    	@Override
    	public boolean onTouchEvent(MotionEvent event) {
    		// 修改坐标
    		currentX = event.getX();
    		currentY = event.getY();
    		
    
    		// 通知组件,重新绘制自己
    		invalidate();
    		// 返回true表明该方法已经处理该事件
    		return true;
    	}
    	
    }
    


    xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root_relativeLayout_id"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
    
        <com.kale.drawview.DrawView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    




  • 相关阅读:
    J2EE系列 (一) 几个技术规范
    MyEclipse 10 优化技巧
    J2EE (二) Servlet设置Session Cookies
    CSS 外层box自动计算高度的问题
    UI设计技巧Div封闭式Div导致页面显示异常
    Windows 7 IIS7 无法启动, 显示WAS & W3SVC没有启动的错误提示
    Windows 7 截图
    GridView技巧增加序号列
    ERWin & ERStudio图里的实线和虚线的含义
    [转]CSS布局口诀 CSS BUG顺口溜
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3852644.html
Copyright © 2020-2023  润新知