当前位置: 首页 > 工具软件 > DrawView > 使用案例 >

自定义组件(DrawView) 提高代码复用性

杜辉
2023-12-01


编写DrawView的代码:

package com.example.drawview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DrawView extends View {

	Paint paint = null;
	float startX = 0;//用来缓冲起始位置x
	float startY = 0;//用来缓冲起始位置y
	float stopX = 0;
	float stopY = 0;
	
	Bitmap cache = null;
	private Canvas cacheCanvas;
	private Matrix matrix;
	/*
	 * XML 生成一个View对象的时候会被调用。且有样式设置的时候调用这个构造函数
	 */
	public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}
	
	/**
	 * xml生成View且没有设置样式主题的时候会被调用
	 */
	public DrawView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	/**
	 * 通过代码创建的时候会被调用
	 */
	public DrawView(Context context) {
		super(context);
		init();
	}
	
	
	//定义一个初始化的方法
	private void init()
	{
		paint = new Paint();
		matrix = new Matrix();
	}
	
	/**
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		
		if(cache == null)
		{
			cache = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
			cacheCanvas = new Canvas(cache);
			//这个方法会把view要显示的内容绘制到界面上
			cacheCanvas.drawColor(0xffaaaaaa);
		}
		
		cacheCanvas.drawLine(startX, startY, stopX, stopY, paint);
		startX = stopX;
		startY = stopY;
		
		canvas.drawBitmap(cache, matrix, paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			startX = event.getX();
			startY = event.getY();
			break;
			
		case MotionEvent.ACTION_MOVE:
			stopX = event.getX();
			stopY = event.getY();
			invalidate();//通知界面绘制内容
			break;
		case MotionEvent.ACTION_UP:
			break;
		default:
			break;
		}
		return true;
	}

}


编写ScondActivity代码:(调用DrawView中的代码)

package com.example.drawview;

import android.app.Activity;
import android.os.Bundle;

public class ScondActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		DrawView view = new DrawView(this);//调用DrawView类,直接使用,提高代码复用性
		this.setContentView(view);
	}
}

最后在MainActivity中调用,使之回调到主Activity上:

package com.example.drawview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void skip(View view)
    {
    	Intent intent = new Intent(this, ScondActivity.class);
    	startActivity(intent);
    }
}

降低各个组件之间的偶合度,提高代码复用性!!!(类似Java EE中的MVC设计模式)




 类似资料: