在android应用程序中加载一张图片,然后把它显示出来这是一件非常容易的事情,那怎么才能显示一张图片的一小部分呢,一种做法是把图片ps一下,要显示的那部分单独存成一张图片,然后程序中加载它,并把它显示出来。但这样会增加程序的图片量。对一张完整的图,用程序去切割你想要的那部分也是很简单的。
下面实现的一个程序,是加载了一张图片,然后经过变换让图片填充到手机的整个屏幕,然后在屏幕的中间显示图片中间的100*100的部分。
ShowPoritionPictureActivity代码:
[java] <span style="font-size:16px;">package com.iwin.zzs;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created. */
Bitmap picRes;
Bitmap showPic;
//获取原图片的宽和高
int picWidth;
int picHeight;
private PoritionView poritonView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 不显示状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// 得到屏幕的长和宽
int screenWidth = dm.widthPixels; //水平分辨率
int screenHeight = dm.heightPixels; //垂直分辨率
picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl);
// 得到图片的长和宽
picWidth = picRes.getWidth();
picHeight = picRes.getHeight();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) screenWidth ) / picWidth;
float scaleHeight = ((float) screenHeight ) / picHeight;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 新得到的图片是原图片经过变换填充到整个屏幕的图片
Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true);
// bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);
// canvas=new Canvas();
// canvas.setBitmap(bitmap);
showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100);
poritonView = new PoritionView(this);
poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50);
setContentView(poritonView);
}
</span>
<span style="font-size:16px;">package com.iwin.zzs;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created. */
Bitmap picRes;
Bitmap showPic;
//获取原图片的宽和高
int picWidth;
int picHeight;
private PoritionView poritonView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 不显示状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// 得到屏幕的长和宽
int screenWidth = dm.widthPixels; //水平分辨率
int screenHeight = dm.heightPixels; //垂直分辨率
picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl);
// 得到图片的长和宽 www.2cto.com
picWidth = picRes.getWidth();
picHeight = picRes.getHeight();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) screenWidth ) / picWidth;
float scaleHeight = ((float) screenHeight ) / picHeight;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 新得到的图片是原图片经过变换填充到整个屏幕的图片
Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true);
// bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);
// canvas=new Canvas();
// canvas.setBitmap(bitmap);
showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100);
poritonView = new PoritionView(this);
poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50);
setContentView(poritonView);
}
</span>
新建PoritionView类代码:
[java] <span style="font-size:16px;">package com.iwin.zzs;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
public class PoritionView extends View {
private Bitmap showPic = null;
private int startX = 0;
private int startY = 0;
public PoritionView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(showPic, startX, startY, null);
}
public void setBitmapShow(Bitmap b, int x, int y)
{
showPic = b;
startX = x;
startY = y;
}
}
</span>
<span style="font-size:16px;">package com.iwin.zzs;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
public class PoritionView extends View {
private Bitmap showPic = null;
private int startX = 0;
private int startY = 0;
public PoritionView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(showPic, startX, startY, null);
}
public void setBitmapShow(Bitmap b, int x, int y)
{
showPic = b;
startX = x;
startY = y;
}
}
</span>
在工程res/drawable里面添加图片gir.png,运行程序的效果是只显示图片的中间100*100的那部分图片。