//布局文件
<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" >
<ImageView
android:id="@+id/iv_src"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
//主配置文件
package com.demo.image;
import android.app.Activity;
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.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载原图
Bitmap bmScr = BitmapFactory.decodeResource(getResources(), R.drawable.photo3);
//创建副本
//1.创建和原图一样的位图对象,该对象中目前是没有内容的,可以比喻为和原图一样的白纸
Bitmap bmCopy = Bitmap.createBitmap(bmScr.getWidth(),bmScr.getWidth(),bmScr.getConfig());
//2.创建画笔对象
Paint paint = new Paint();
//3.创建画板 ,把白纸写进来
Canvas canvas = new Canvas(bmCopy);
//4.开始作画
//arg1:图片矩阵 作画的时候需要矩阵,现在不需要,所以直接new出来就可以了
canvas.drawBitmap(bmScr, new Matrix(), paint);
ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
iv_src.setImageBitmap(bmScr);
ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
iv_copy.setImageBitmap(bmScr);
}
}
本文介绍了一个简单的Android应用程序,用于演示如何在Android中通过Bitmap对象复制并绘制图像。具体步骤包括:加载原始图像,创建与原图大小相同的空白Bitmap对象作为副本,使用Canvas和Paint对象将原图绘制到副本上,并在界面上显示原图及其副本。
27

被折叠的 条评论
为什么被折叠?



