<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 fl.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint();//定义一个画笔 Bitmap bitmap=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.background); canvas.drawBitmap(bitmap, 0, 0, paint);//绘制背景图像 Bitmap bitmap_img1=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.img1); canvas.drawBitmap(bitmap_img1, 0, 0, paint);//绘制原图 //应用setRotate(float degress)方法旋转图像 Matrix matrix=new Matrix(); matrix.setRotate(30);//以(0,0)点为轴心旋转30度 canvas.drawBitmap(bitmap_img1, matrix, paint);//绘制图像并应用matrix的变换 //应用setRotate(float degrees,float px,float py)方法旋转图像 Matrix m=new Matrix(); m.setRotate(90, 187, 187); canvas.drawBitmap(bitmap_img1, m, paint); super.onDraw(canvas); } } }
二.缩放图像
使用Android提供的android.graphics.Matrix类的setScale()、postScale()和preScale()方法,可以对图像进行缩放。<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 fl.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint();//定义一个画笔 paint.setAntiAlias(true);//设置抗锯齿 Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.backgroud); canvas.drawBitmap(bitmap_bg, 0, 0, paint);//绘制背景 Bitmap bitmap_rabbit=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.rabbit); //应用setScale(float sx,float sy)方法缩放图像 Matrix m1=new Matrix(); m1.setScale(2f, 2f);//以(0,0)点为轴心在X轴和Y轴上均缩放200% canvas.drawBitmap(bitmap_rabbit, m1, paint);//绘制图像并应用matrix的变换 //应用setScale(float sx,float sy,float px,float py)方法缩放图像 Matrix m2=new Matrix(); m2.setScale(0.4f, 0.4f,156,156);//以(156,156)点为轴心在X轴和Y轴上均缩放40% canvas.drawBitmap(bitmap_rabbit, m2, paint);//绘制图像并应用matrix的变换 canvas.drawBitmap(bitmap_rabbit, 0, 0, paint);//绘制原图 super.onDraw(canvas); } } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 fl.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint();//定义一个画笔 paint.setAntiAlias(true);//设置抗锯齿 Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.backgroud); canvas.drawBitmap(bitmap_bg, 0, 0, paint);//绘制背景 Bitmap bitmap_rabbit=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.rabbit); //应用setSkew(float kx,float ky)方法倾斜图像 Matrix m1=new Matrix(); m1.setSkew(2f, 1f);//以(0,0)点为轴心,在X轴上倾斜2,在Y轴上倾斜1的图像 canvas.drawBitmap(bitmap_rabbit, m1, paint);//绘制图像并应用matrix的变换 //应用setSkew(float kx,float ky,float px,float py)方法倾斜图像 Matrix m2=new Matrix(); m2.setSkew(-0.5f, 0f,178,169);//以(178,169)为轴心,在X轴上倾斜-0.5的图像 canvas.drawBitmap(bitmap_rabbit, m2, paint);//绘制图像并应用matrix的变换 canvas.drawBitmap(bitmap_rabbit, 0, 0, paint);//绘制原图 super.onDraw(canvas); } } }
四.平移图像
使用Android提供的android.graphics.Matrix类的setTranslate()、postTranslate()和preTranslate()方法,可对图像进行平移。<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 fl.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint();//定义一个画笔 paint.setAntiAlias(true);//设置抗锯齿 Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.backgroud); canvas.drawBitmap(bitmap_bg, 0, 0, paint);//绘制背景 Bitmap bitmap_rabbit=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.rabbit); canvas.drawBitmap(bitmap_rabbit, 0, 0, paint);//绘制原图 //应用setSkew(float kx,float ky)方法倾斜图像 Matrix m1=new Matrix(); m1.setRotate(30); m1.postTranslate(100, 50); canvas.drawBitmap(bitmap_rabbit, m1, paint);//绘制图像并应用matrix的变换 super.onDraw(canvas); } } }
运行实例,效果如图所示
转载请注明出处:http://blog.csdn.net/acmman/article/details/45601821