标签:
转载自http://blog.csdn.net/csxwc/article/details/10345235
Bitmap是Android系统中的图像处理的最重要的类之一。用它可以获取图像文件信息,对图像进行旋转,剪切,放大,缩小等操作。
Bitmap代表一张位图,使我们在开发中常用的资源,下面就对Bitmap进行简单的介绍。
public class MainActivity extends Activity {
String[] images = null;
//获取访问assets文件的对象
AssetManager assets = null;
int currentImg = 0;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img_show);
try {
//获取访问assets下文件的对象
assets = getAssets();
images = assets.list("");
} catch (Exception e) {
e.printStackTrace();
}
Button btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果角标越界
if(currentImg>=images.length){
currentImg = 0;
}
//找到下一张图片
while(!images[currentImg].endsWith(".jpg")){
currentImg++;
if(currentImg>=images.length){
currentImg = 0;
}
}
InputStream assetFile = null;
try {
//打开指定资源对应的输入流
assetFile = assets.open(images[currentImg++]);
} catch (Exception e) {
e.printStackTrace();
}
//回收图片
BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
if(bitmapDrawable!=null&&!bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
//显示图片
img.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View{
//记录背景位图的实际高度
final int BACK_HEIGHT = 1700;
//背景图片
private Bitmap back;
private Bitmap plane;
//背景图片的开始位置
final int WIDTH = 320;
final int HEIGHT = 440;
private int startY = BACK_HEIGHT -HEIGHT;
public MyView(Context context) {
super(context);
back = BitmapFactory.decodeResource(context.getResources(), R.drawable.back_img);
plane = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane);
final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what ==0x123){
//重新开始移动
if(startY<=0){
startY = BACK_HEIGHT - HEIGHT;
}
else
startY -= 3;
}
invalidate();
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0x123);
}
}, 0,100);
}
@Override
protected void onDraw(Canvas canvas) {
//根据原始位图和Matrix创建新的图片
Bitmap bitmap_2 = Bitmap.createBitmap(back, 0, startY, WIDTH, HEIGHT);
//绘制新位图
canvas.drawBitmap(bitmap_2, 0,0,null);
//绘制飞机
canvas.drawBitmap(plane, 160, 380,null);
}
}
}
Matrix matrix = new Matrix();
//镜面效果
matrix.setScale(-1, 1);
matrix.postTranslate(bitmap.getWidth(), 0);
//倒影效果
matrix.setScale(1, -1);
matrix.postTranslate(0, bitmap.getHeight());
//设置重叠部分的效果
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_ATOP));
canvas.drawBitmap(bitmapa, new Matrix(), paint);
canvas.drawBitmap(bitmapb, new Matrix(),paint);
Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
标签:
原文地址:http://www.cnblogs.com/qtfan/p/4701198.html