Android UI, canvas, drawable

1. UI 和 UI 组件 

view  和 view group ,  view group 能add view, 比如像layout

所有UI组件都继承于view, 靠 onDraw 方法将控件画出来


UI学习分为两部分:  UI的呈现 和 UI的事件响应

事件主要分为: keyEvent 和 touchEvent


事件的响应分为两部分: 注册事件监听器(外),比如setonXXXXX ; 扩展View的时候回调事件函数(内)。。



使用 Canvas 自己画控件

selfbutton.java   自定义控件,  继承自 View,  画的动作在onDraw方法中

package com.example.selfview;

import com.example.uitest.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class SelfButton extends View {

	public SelfButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		canvas.drawColor(Color.GRAY);
		canvas.save();  //保存这个时间点的canvas设置
		
		//设置矩阵,  旋转 变形等
		Matrix matrix = new Matrix();
		matrix.setRotate(10.0f);
		canvas.setMatrix(matrix);
		
		
		//裁剪
		//canvas.clipRect(5, 5, 300, 300);
		
		Paint paint = new Paint();
		paint.setColor(Color.RED);
		RectF r2=new RectF();                           //RectF对象  
	    r2.left=50;                                 //左边  
	    r2.top=50;                                 //上边  
	    r2.right=450;                                   //右边  
	    r2.bottom=600;                              //下边  
	    canvas.drawRoundRect(r2, 30, 30, paint);        //绘制圆角矩形  
	    
	    //还原canvas设置
	    canvas.restore();
		paint.setColor(Color.GREEN);
		paint.setTextSize(50);
		canvas.drawText("hello world", 50, 50, paint);
		canvas.drawRect(10, 10, 190, 190, paint);
		
		//画位图
		Resources res = getResources();
		Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.google);
		Rect dst = new Rect(50, 50, 200, 200);
		canvas.drawBitmap(bmp, null, dst, paint);
		
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.i("myLog", "myaction: " + event.getAction());
		
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			Log.i("myLog", "myaction: down");
		}
		
		return super.onTouchEvent(event);
	}
	
}

activity

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SelfButton(this));
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		//长按键触发
		if(event.getAction() == MotionEvent.ACTION_UP){
			long time =  event.getEventTime()-event.getDownTime();
			Log.i("myLog", "myaction: time " + time);
		}
		return super.onTouchEvent(event);
	}

}

使用drawable 画图形

public class MyDrawableView extends View {

	private BitmapDrawable bmpDrawable;
	
	public MyDrawableView(Context context) {
		super(context);
		bmpDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.google);
		bmpDrawable.setBounds(10, 10, 100, 100);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		bmpDrawable.draw(canvas);
	}

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值