View
package com.restore;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;public class myview extends View { Path mPath; Bitmap mBitmap; Canvas mCanvas; Paint mPaint; int width; int height; int color; float mX = 0.0f; float mY = 0.0f; private Bitmap cachebBitmap; @Override public boolean onTouchEvent(MotionEvent event) { int act = event.getAction(); float x = event.getX(); float y = event.getY(); switch (act) { case MotionEvent.ACTION_DOWN: mPath.moveTo(x, y); break; case MotionEvent.ACTION_MOVE: mPath.quadTo(mX, mY, x, y); break; case MotionEvent.ACTION_UP: mCanvas.drawPath(mPath, mPaint); mPath.reset(); break; } invalidate(); mX = x; mY = y; return true; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // mcanvas = new Canvas(mBitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(cachebBitmap, 0, 0, null); canvas.drawPath(mPath, mPaint); } public void clear() { mCanvas.drawColor(Color.WHITE); mCanvas.drawBitmap(cachebBitmap, 0, 0, null); invalidate(); } public void setColor(int c){ mPaint.setColor(c); } public void setBlock(float size) { mPaint.setStrokeWidth(size); } public myview(Context context) { super(context); init(); } public myview(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public myview(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void init(){ DisplayMetrics metrics = new DisplayMetrics(); WindowManager wm = (WindowManager) this.getContext().getSystemService("window"); wm.getDefaultDisplay().getMetrics(metrics); width = metrics.widthPixels; height = metrics.heightPixels; mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(12); cachebBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(cachebBitmap); mPath = new Path(); }}
ACTIVITY
package com.restore;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;public class TestRestoreActivity extends Activity implements OnClickListener { Path mPath; Bitmap mBitmap; Canvas mCanvas; Paint mPaint; int width; int height; myview m; Button l1, l2, l3,cred,cbule,cblack; Button colorBtn = null; Button clearBtn = null; Button lineBtn = null; LinearLayout linelayout, colorlayout = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); height = getWindowManager().getDefaultDisplay().getHeight(); width = getWindowManager().getDefaultDisplay().getWidth(); m = (myview) findViewById(R.id.myview); linelayout = (LinearLayout) findViewById(R.id.lineBar); colorlayout = (LinearLayout) findViewById(R.id.colorBar); colorBtn = (Button) findViewById(R.id.color); lineBtn = (Button) findViewById(R.id.line); clearBtn = (Button) findViewById(R.id.clear); cred = (Button) findViewById(R.id.colorred); cbule = (Button) findViewById(R.id.colorblue); cblack = (Button) findViewById(R.id.colorblack); l1 = (Button) findViewById(R.id.l1); l3 = (Button) findViewById(R.id.l3); l2 = (Button) findViewById(R.id.l2); colorBtn.setOnClickListener(this); lineBtn.setOnClickListener(this); clearBtn.setOnClickListener(this); l1.setOnClickListener(this); l2.setOnClickListener(this); l3.setOnClickListener(this); cred.setOnClickListener(this); cbule.setOnClickListener(this); cblack.setOnClickListener(this); } @Override public void onClick(View v) { if (v == colorBtn) { if (!colorlayout.isShown()) { if (linelayout.isShown()) { linelayout.setVisibility(View.INVISIBLE); } colorlayout.setVisibility(View.VISIBLE); } else { colorlayout.setVisibility(View.INVISIBLE); } } else if (v == lineBtn) { if (!linelayout.isShown()) { if (colorlayout.isShown()) { colorlayout.setVisibility(View.INVISIBLE); } linelayout.setVisibility(View.VISIBLE); } else { linelayout.setVisibility(View.INVISIBLE); } } else if (v == clearBtn) { m.clear(); } else if (v == cbule) { m.setColor(Color.BLUE); colorlayout.setVisibility(View.INVISIBLE); } else if (v == cred) { m.setColor(Color.RED); colorlayout.setVisibility(View.INVISIBLE); } else if (v == cblack) { m.setColor(Color.BLACK); colorlayout.setVisibility(View.INVISIBLE); } else if(v == l1) { m.setBlock(12.0f); linelayout.setVisibility(View.INVISIBLE); } else if(v == l2) { m.setBlock(24.0f); linelayout.setVisibility(View.INVISIBLE); } else if(v == l3) { m.setBlock(36.0f); linelayout.setVisibility(View.INVISIBLE); } }}
mainfest
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.restore" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".TestRestoreActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>