Android实现自定义view---绘制图片
时间:2014-05-10 09:23:24
收藏:0
阅读:601
主要原理是:extends view重载onDrow(),需要一bitmap,重载view,在 canvas上画临时bmp,当按下确定的时候按下
画到real_bmp上,其他的跟前面的基本上没有什么区别。
import com.example.test.R; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.os.Bundle; import android.provider.ContactsContract.CommonDataKinds.Event; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class bmpeffect extends Activity{ /* * 需要一bitmap,重载view,在 canvas上画临时bmp,当按下确定的时候按下 * 画到real_bmp上,其他的跟前面的基本上没有什么区别 */ private static Bitmap src; private static Bitmap heart; private static Bitmap star; private static Bitmap star2; private static int choose; private int height; private int width; public static float bmp_x; public static float bmp_y; private Canvas my_canvas; public void onCreate(Bundle save){ Log.e("on create", "hello"); super.onCreate(save); src=data.src_bmp; if(data.src_bmp==null) Log.e("bmp", "null"); my_canvas=new Canvas(data.src_bmp); height=data.src_bmp.getHeight(); width=data.src_bmp.getWidth(); heart=data.getbmp(getResources(), R.drawable.small_heart); star=data.getbmp(getResources(), R.drawable.small_star); star2=data.getbmp(getResources(), R.drawable.star8); choose=0; setContentView(R.layout.bmptest); } public void on_star2(final View v){ //star8; choose=3; } public void on_star(final View v){ choose=2; } public void on_heart(final View v){ choose=1; } public void on_ok(final View v){ drow_bmp(my_canvas); } public static void drow_bmp(Canvas canvas){ switch(choose){ case 1: canvas.drawBitmap(heart, bmp_x, bmp_y, null); break; case 2: canvas.drawBitmap(star, bmp_x,bmp_y, null); break; case 3: //canvas.drawBitmap(star2, matrix, paint) canvas.drawBitmap(star2, bmp_x, bmp_y, null); } } public static class bmpview extends View{ public bmpview(Context context,AttributeSet attrs) { super(context,attrs); // TODO Auto-generated constructor stub } protected void onDraw(Canvas canvas){ canvas.drawColor(0xff000000); Log.i("canvas", "drow"); if(src!=null){ canvas.drawBitmap(src, 0, 0, null); } drow_bmp(canvas); } public boolean onTouchEvent(MotionEvent event){ int action=event.getAction(); //现在应该可以不用区分不同的手势事件了,只需要得到 bmp_x=event.getX(); bmp_y=event.getY(); invalidate(); return true; } } }
效果如下
评论(0)