android之图像的操作

来源:互联网 发布:淘宝充话费很久不到账 编辑:程序博客网 时间:2024/09/21 11:16

要想处理图像,必须经过以下步骤:

1.拿到原图

2.拿到一张与原图相同的纸

3.然后将这张纸固定在画板上

4.找一根画笔

5.按照一定规则处理原图图片

6.将处理过的图片用画笔画出来

代码编写:

布局:

<LinearLayout 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:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="操作图片"        android:onClick="btn"/>    <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/iv1"        android:src="@drawable/img_small_1"/>    <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/iv2"        android:background="#000000"/></LinearLayout>
activity:

public class MainActivity extends Activity {private ImageView iv2;//原图private Bitmap baseBitmap;//画纸private Bitmap copyBitmap;//画板private Canvas canvas;//画笔private Paint paint;public void btn(View view) {// TODO Auto-generated method stub//拿到原图baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img_small_1);//1.先拿到一张与原图相同的纸        copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());//2.然后将这张纸固定在画板上        canvas=new Canvas(copyBitmap);//3.找一根画笔        paint=new Paint();//4.按照一定规则处理原图图片        Matrix matrix=new Matrix();//1:1          //缩放          //matrix.setScale(0.5f, 0.5f);          //位移          //matrix.setTranslate(50f, 50f);          //旋转          //matrix.setRotate(45);//顺时针旋转45度,默认以左上角为原点          //matrix.setRotate(45, baseBitmap.getWidth()/2, baseBitmap.getHeight()/2);//沿中心点旋转          //镜面效果          matrix.setScale(-1f, 1f);          //matrix.setTranslate(baseBitmap.getWidth(),0);          matrix.postTranslate(baseBitmap.getWidth(), 0);//如果要对图片进行多次操作,要用postTranslate          //倒影          matrix.setScale(1f, -1f);          matrix.postTranslate(0, baseBitmap.getHeight());//5.将处理过的图片用画笔画出来canvas.drawBitmap(baseBitmap, matrix, paint);iv2.setImageBitmap(copyBitmap);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv2=(ImageView) findViewById(R.id.iv2);}}

效果图:









0 0
原创粉丝点击