Android---響應button事件,onDraw畫圖(直線、三角形、矩形等)
阿新 • • 發佈:2019-02-04
最近面試的一些公司都在問有沒有學過繪圖,以前真沒怎麼接觸過。現學現賣下。。。
1.main.xml裡面的程式碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_margin="2dp" android:layout_width="wrap_content" android:layout_height="fill_parent"> <Button android:text="繪製直線" android:layout_width="150dp" android:layout_height="76dp" android:onClick="doCreateLine" /> <Button android:text="繪製三角形" android:layout_width="150dp" android:layout_height="76dp" android:onClick="doCreateTriangle" /> <Button android:text="繪製矩形" android:layout_width="150dp" android:layout_height="76dp" android:onClick="doCreateRect" /> </LinearLayout> <LinearLayout android:background="#FFFFFFFF" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
2.主Activity程式碼:
package com.bison; import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; public class ShapeDemoActivity extends Activity { private DrawCG mDrawCG; private ImageView imgView; public void init() { // 獲取手機視窗的大小 WindowManager wm = getWindowManager(); Display display = wm.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); imgView = (ImageView) findViewById(R.id.imgView); mDrawCG = new DrawCG(ShapeDemoActivity.this, screenWidth - 150, screenHeight); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } // 按鈕事件 public void doCreateLine(View view) { imgView.setImageBitmap(mDrawCG.drawLine()); } public void doCreateTriangle(View view) { imgView.setImageBitmap(mDrawCG.drawTriangle()); } public void doCreateRect(View view) { imgView.setImageBitmap(mDrawCG.drawRect()); } }
3.畫圖類
package com.bison; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.graphics.Rect; import android.view.View; class DrawCG extends View { private Paint paint; private Canvas canv; private Bitmap mBitmap; public DrawCG(Context context, int width, int height) { super(context); // 宣告畫筆 paint = new Paint(); // 設定顏色 paint.setColor(Color.RED); // 設定抗鋸齒 paint.setAntiAlias(true); // 設定線寬 paint.setStrokeWidth(3); // 設定非填充 paint.setStyle(Style.STROKE); // 宣告點陣圖 mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); // 宣告畫布 canv = new Canvas(mBitmap); } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(mBitmap, 0, 0, null); // super.onDraw(canvas); } /** * 畫線 * * @return */ public Bitmap drawLine() { canv.drawLine(200, 50, 600, 50, paint); return mBitmap; } /** * 畫三角形 * * @return */ public Bitmap drawTriangle() { Path path = new Path(); path.moveTo(300, 600); path.lineTo(600, 200); path.lineTo(900, 600); path.lineTo(300, 600); canv.drawPath(path, paint); return mBitmap; } /** * 畫矩形 * * @return */ public Bitmap drawRect() { canv.drawRect(new Rect(150, 150, 500, 500), paint); return mBitmap; } }
PS:實現了,點選左邊按鈕,右邊會出現要繪製的圖形。現在該考慮如何實現動態調整圖形的大小和位置了。繼續努力,加油。