Android 图片缩放

以下演示将一个ImageView的高度设置为两倍:

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    <ImageView
            android:id="@+id/image2"
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
</LinearLayout>

Activity:

package com.example.imageResize;

import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.widget.ImageView;

public class MyActivity extends Activity {
    private ImageView imageView1;
    private ImageView imageView2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.imageView1 = (ImageView) findViewById(R.id.image1);
        this.imageView2 = (ImageView) findViewById(R.id.image2);

        //从一个文件中获取bitmap对象
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        imageView1.setImageBitmap(bitmap);

        //创建一个新的bitmap
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight()*2, bitmap.getConfig());
        //创建这个图片的画板
        Canvas canvas = new Canvas(bitmap2);

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);

        Matrix matrix = new Matrix();
        matrix.setScale(1,2);

        //以之前的bitmap作为模板,长度不变,高度变为两倍
        canvas.drawBitmap(bitmap,matrix,paint);

        imageView2.setImageBitmap(bitmap2);
    }
}

 

posted @ 2014-04-10 21:28  无忧之路  阅读(194)  评论(0编辑  收藏  举报
无忧之路