运行截图如下图所示
1.画三角形类
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.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class Triangle extends View {
private int mStartX;
private int mStartY;
private Path mPath;
private Paint mPaint;
private float point_x_3;
private float point_y_3;
private float point_x_2;
private float point_y_2;
int VIEW_WIDTH = 1280;
int VIEW_HEIGHT = 750;
// 定义一个内存中的图片,该图片将作为缓冲区
Bitmap cacheBitmap ;
// 定义cacheBitmap上的Canvas对象
Canvas cacheCanvas = null;
public Triangle(Context context, AttributeSet attrs) {
super(context, attrs);
mPath = new Path();
mPaint = new Paint();
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas = new Canvas();
//设置画笔的颜色
mPaint.setColor(Color.YELLOW);
//设置背景色
cacheBitmap.eraseColor(Color.RED);
// 设置cacheCanvas将会绘制到内存中的cacheBitmap上
cacheCanvas.drawBitmap(cacheBitmap, VIEW_WIDTH, VIEW_HEIGHT, null);
mPaint.setStrokeWidth(10);
mPaint.setAntiAlias(true);
// 设置Paint的样式
mPaint.setStyle(Paint.Style.STROKE);
// 设置结合处为圆弧
mPaint.setStrokeJoin(Paint.Join.ROUND);
// 设置画笔样式为圆弧
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartX = (int) event.getX();
mStartY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getX();
float y = event.getY();
float deltaX = x - mStartX;
float deltaY = y - mStartY;
float frac = (float) Math.sqrt(3) / 2;
point_x_2 = x;
point_y_2 = y;
point_x_3 = mStartX + (float) (Math.sqrt(1 - Math.pow(frac, 2)) * deltaX - frac * deltaY);
point_y_3 = mStartY + (float) (Math.sqrt(1 - Math.pow(frac, 2)) * deltaY + frac * deltaX);
break;
case MotionEvent.ACTION_UP:
mPath.reset();
mPath.moveTo(mStartX, mStartY);
mPath.lineTo(point_x_2, point_y_2);
mPath.lineTo(point_x_3, point_y_3);
mPath.close();
break;
default:
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
}
}
2.activity类
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.mmmm.sanjiaox.Triangle
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:width="1280dp"
android:height="1280dp"
/>
</RelativeLayout>