写一个activity,实现:1.使用Android的图形处理的基本操作技能 a)绘制图片500×500,左空隙10dp,在图片底部添上文字“Hello Android” b)绘制点(10,550),(800,1000),点宽50,颜色为黄色; c)绘制绿色线(10,550)到(800,1000),线宽10;红色多线(10,550)到(10,1000), (10,1000)到(800,1000) ,(800,1000)到(800,550) ,(800,550)到(10,550) d)绘制矩形,填充色CYAN, (10, 1050, 350, 1500)
时间: 2024-02-13 18:07:35 浏览: 124
好的,以下是一个简单的Activity代码,实现了你提到的图形处理的基本操作技能:
```
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class DrawActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
}
private static class DrawView extends View {
private Paint mPaint;
public DrawView(DrawActivity context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mPaint.setStrokeWidth(50);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制图片
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(30);
canvas.drawText("Hello Android", 10, 510, mPaint);
canvas.drawBitmap(null, 10, 10, mPaint);
// 绘制点
mPaint.setColor(Color.YELLOW);
canvas.drawPoint(10, 550, mPaint);
canvas.drawPoint(800, 1000, mPaint);
// 绘制绿色线
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(10);
canvas.drawLine(10, 550, 800, 1000, mPaint);
// 绘制红色多线
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(5);
Path path = new Path();
path.moveTo(10, 550);
path.lineTo(10, 1000);
path.lineTo(800, 1000);
path.lineTo(800, 550);
path.close();
canvas.drawPath(path, mPaint);
// 绘制矩形
mPaint.setColor(Color.CYAN);
Rect rect = new Rect(10, 1050, 350, 1500);
canvas.drawRect(rect, mPaint);
}
}
}
```
在Activity中,我们创建了一个自定义的View(DrawView),然后在onDraw方法中使用Canvas和Paint类来实现各种图形的绘制。注意,这里的绘制操作是在UI线程中进行的,因此对于比较复杂或者需要频繁刷新的图形,最好在单独的线程中进行绘制,以避免UI线程阻塞。
阅读全文