前面学习已知,Android大部分组件都是View的子类,而如果要进行图形的绘制操作,则我们可以用一个类去继承View类,之后覆写View类中指定方法。

View中绘图方法

protected void onDraw(Canvas canvas)

protected void onDrawScrollBars(Canvas canvas)

绘图4个核心操作类

android.graphics.Canvas类:操作绘图,绘图平台,提供了一个画板功能

android.graphics.Paint类:相当于画笔类

android.graphics.Bitmap:图片管理类,操作图片资源

android.graphics.Matrix: 矩阵类

现在我们先画几个简单图形,了解一下这几个类的用法。

定义MyView类:

public class MyView extends View{public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}    protected void onDraw(Canvas canvas){    canvas.drawColor(Color.WHITE);//设置背景颜色    Paint paint=new Paint();    paint.setColor(Color.YELLOW);//设置笔尖为黄色    canvas.drawCircle(30,50,25,paint);//画圆    paint.setColor(Color.BLACK);    canvas.drawRect(80,20,160,80, paint);//定义矩形    Rect rect=new Rect();//定义矩形    rect.set(180, 20, 200, 80);//设置矩形大小    paint.setStyle(Style.STROKE);//空心显示    canvas.drawRect(rect,paint);    paint.setColor(Color.BLUE);    paint.setTextSize(20);    canvas.drawText("ee的画板", 10, 110, paint);//显示文字    paint.setColor(Color.RED);    canvas.drawLine(10, 120, 300, 120, paint);    RectF oval=new RectF();//定义参考矩形    oval.set(10.0f, 140.0f, 110.0f, 200.0f);    canvas.drawOval(oval, paint);//画椭圆    oval.set(150.0f, 140.0f, 110.0f, 200.0f);//定义大小    canvas.drawArc(oval, 150.0f, 140.0f, true, paint);//画弧    //绘图    Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.wall);    //消除锯齿    paint.setAntiAlias(true);    canvas.drawBitmap(bitmap, 200, 200,paint);    paint.setTextSize(20);    paint.setColor(Color.BLACK);    canvas.drawText("图片高度:"+bitmap.getHeight()+", 图片宽度:"+bitmap.getWidth(),    10, bitmap.getHeight()+20, paint);                       }}

然后在布局文件添加我们自己定义的组件 MyView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MyView" >    <com.example.viewdraw.MyView         android:id="@+id/myview"          android:layout_width="fill_parent"    android:layout_height="fill_parent"                                                    /></LinearLayout>

接下来,Activity里面只需要显示即可,这里是不动的,系统生成默认。

实现效果如图:



Android图形绘制



接下来,我们深入运用一下Bitmap

首先我们定义将图片拉伸。

加入以下几行代码:

/*     * 将图片拉伸至手机屏幕大小     */    DisplayMetrics dm=getResources().getDisplayMetrics();    int screenWidth=dm.widthPixels;    int screenHeight=dm.heightPixels;    bitmap=Bitmap.createScaledBitmap(bitmap, screenWidth, screenHeight, true);    canvas.drawBitmap(bitmap, 0, 0, paint);


Android图形绘制

然后我们定义图片在指定区域显示,也就是定义一个矩形框

加上这个代码

canvas.drawBitmap(bitmap, null, new Rect(30,50,200,200), paint);


Android图形绘制

Android图形绘制

更多相关文章

  1. Android 自定义ContentProvider
  2. [转] 移植Android图形引擎Skia到MIPS平台经验总结(一)
  3. android 自定义标题栏和自定义下拉选项PopupWindow
  4. Android:绘制自定义视图
  5. AndroidManifest--定义android清单
  6. android中自定义控件的属性

随机推荐

  1. Android取消EditText自动聚焦、自动弹出
  2. Android两种数据库操作方式入门介绍
  3. Android中实现Broastcast接收短信
  4. Android(安卓)Studio之工程中导入jni库方
  5. FFMPEG for android
  6. Android采用KSOAP2访问webservice
  7. Android2.2应用解析
  8. 浅谈Android五大布局
  9. 详解 Android(安卓)的 Activity 组件
  10. Ubuntu下进行Android开发的相关配置