Android Graphic demo
How to draw images? Here is a demo for your reference.
1. Take a look at the engineering structure:
Ii. Custom view the custom view implements the tracking retention function. The Code is as follows:
Package picturegame. view; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import com. winton. picturegame. r; public class GameView extends View {private Paint paint = null; // private Bitmap originalBitmap = null; // original map private Bitmap new1Bitmap = null; private Bitmap new2Bitmap = null; private float clickX = 0; private float clickY = 0; private float startX = 0; private float startY = 0; private boolean isMove = true; private boolean isClear = false; private int color = Color. RED; // The default paint brush color is RED private float strokeWidth = 2.0f; // The default paint brush width public GameView (Context context) {this (context, null ); // TODO Auto-generated constructor stub} public GameView (Context context, AttributeSet atts) {this (context, atts, 0 ); // TODO Auto-generated constructor stub} public GameView (Context context, AttributeSet atts, int defStyle) {super (context, atts, defStyle ); // TODO Auto-generated constructor stuboriginalBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. default_pic ). copy (Bitmap. config. ARGB_8888, true); // loads a background new1Bitmap = originalBitmap. createBitmap (originalBitmap);} // clear the public void clear () {isClear = true; new2Bitmap = originalBitmap. createBitmap (originalBitmap); invalidate (); // overload} public void setStrokeWidth (float width) {this. strokeWidth = width; initPaint () ;}@ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); canvas. drawBitmap (writer (new1Bitmap), 0, 0, null) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stubclickX = event. getX (); clickY = event. getY (); if (event. getAction () = MotionEvent. ACTION_DOWN) {isMove = false; invalidate (); return true;} else if (event. getAction () = MotionEvent. ACTION_MOVE) {isMove = true; invalidate (); return true;} return super. onTouchEvent (event);}/*** @ Title: writer * @ Description: TODO (generate bitmap) * @ param pic * @ param @ return setting file * @ return Bitmap return type * @ throws */public Bitmap writer (Bitmap pic) {initPaint (); canvas canvas = null; if (isClear) {canvas = new Canvas (new2Bitmap);} else {canvas = new Canvas (pic);} if (isMove) {canvas. drawLine (startX, startY, clickX, clickY, paint); // dashes} startX = clickX; startY = clickY; if (isClear) {return new2Bitmap;} return pic ;} private void initPaint () {paint = new Paint (); // creates a paint brush. setStyle (Style. STROKE); // set it to draw line paint. setAntiAlias (true); // You can smooth the line with paint. setColor (color); // you can specify the paint color. setStrokeWidth (strokeWidth); // set the width of the paint brush line}/*** @ Title: setColor * @ Description: TODO (interface for setting the line color) * @ param color setting file * @ return void return type * @ throws */public void setColor (int color) {this. color = color; initPaint ();}}
3. Homepage layout file the homepage layout File
4. Main Activity code
package com.winton.picturegame;import picturegame.view.GameView;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.winton.basemodule.BaseActivity;public class MainActivity extends BaseActivity implements OnClickListener {private GameView gameview = null;private Button clear = null;private TextView tv30,tv25,tv20,tv15,tv10,tv5,tv2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic void initView() {// TODO Auto-generated method stubsetContentView(R.layout.activity_main);gameview=(GameView)findViewById(R.id.gameview);clear =(Button)findViewById(R.id.btn_clear);tv30=(TextView)findViewById(R.id.tv_30);tv25=(TextView)findViewById(R.id.tv_25);tv20=(TextView)findViewById(R.id.tv_20);tv15=(TextView)findViewById(R.id.tv_15);tv10=(TextView)findViewById(R.id.tv_10);tv5=(TextView)findViewById(R.id.tv_5);tv2=(TextView)findViewById(R.id.tv_2);}@Overridepublic void initListener() {// TODO Auto-generated method stubclear.setOnClickListener(this);tv30.setOnClickListener(this);tv25.setOnClickListener(this);tv20.setOnClickListener(this);tv15.setOnClickListener(this);tv10.setOnClickListener(this);tv5.setOnClickListener(this);tv2.setOnClickListener(this);}@Overridepublic void initData() {// TODO Auto-generated method stub}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v==clear){gameview.clear();return;}if(v==tv30){gameview.setStrokeWidth(30f);return;}if(v==tv25){gameview.setStrokeWidth(25f);return;}if(v==tv20){gameview.setStrokeWidth(20f);return;}if(v==tv15){gameview.setStrokeWidth(15f);return;}if(v==tv10){gameview.setStrokeWidth(10f);return;}if(v==tv5){gameview.setStrokeWidth(5f);return;}if(v==tv2){gameview.setStrokeWidth(2f);return;}}}
5. The effect is as follows:
6. Question: When the line turns rough, the line may be discontinuous. How can this problem be solved? I guess the algorithm should be run, but I still don't know how to run it.