最近面试的一些公司都在问有没有学过绘图,以前真没怎么接触过。现学现卖下。。。
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:实现了,点击左边按钮,右边会出现要绘制的图形。现在该考虑如何实现动态调整图形的大小和位置了。继续努力,加油。