Android---响应button事件,onDraw画图(直线、三角形、矩形等)

最近面试的一些公司都在问有没有学过绘图,以前真没怎么接触过。现学现卖下。。。

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:实现了,点击左边按钮,右边会出现要绘制的图形。现在该考虑如何实现动态调整图形的大小和位置了。继续努力,加油。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值