复制图片:
package com.example.photocopy;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView im1,im2;
private Bitmap srcbitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im1=(ImageView) findViewById(R.id.image1);
im2=(ImageView) findViewById(R.id.image2);
}
@SuppressLint("NewApi")
public void click(View view){
BitmapFactory.Options op=new Options();
op.inJustDecodeBounds=true;
Bitmap bp=BitmapFactory.decodeResource(getResources(), R.drawable.icon,op);
//获取图片大小,按照一定比例缩放
int photoWidth=op.outWidth;
int photoHeight=op.outHeight;
//获取窗口管理类
WindowManager ma=getWindowManager();
Point point=new Point();
//接收显示屏幕宽高
ma.getDefaultDisplay().getSize(point);
int screenWidth=point.x;
int screenHeight=point.y;
//求出比例
float dy=photoHeight/screenHeight;
float dx=photoWidth/screenWidth;
float scale=(float) 1.0;
if(dy>dx&&dy>1){
//按照垂直缩放
scale=dy;
}
if(dx>dy&&dx>1){
//按照水平缩放
scale=dx;
}
op.inSampleSize = (int) scale;
op.inJustDecodeBounds=false;
srcbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon, op);
im2.setImageBitmap(srcbitmap);
}
}
图片复制,放大
package com.example.photocopy;
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.view.View;
import android.widget.ImageView;
public class SecondActivity extends Activity {
private ImageView im1,im2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
im1=(ImageView) findViewById(R.id.im2);
im1.setImageResource(R.drawable.ic_launcher);
im2=(ImageView) findViewById(R.id.im3);
}
public void click(View view){
//获取原图
Bitmap srcbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//创建可修改的副本
Bitmap alterbitmap=Bitmap.createBitmap(srcbitmap.getWidth(),
srcbitmap.getHeight(), srcbitmap.getConfig());
//创建画布
Canvas canvas=new Canvas(alterbitmap);
//创建画笔
Paint paint=new Paint();
//设置图片矩阵
Matrix matrix=new Matrix();
matrix.setScale(1.0f, 1.0f);
//paint.setColor(color);
//画画
canvas.drawBitmap(srcbitmap, matrix, paint);
im2.setImageBitmap(alterbitmap);
}
}