Android開發ImageUtils工具演示

jopen 11年前發布 | 27K 次閱讀 Android Android開發 移動開發

該工具提供縮放   drawable轉換bitmap   轉換倒影圖     轉換成圓角圖

  

package com.nailsoul.imagedemo.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

public class ImageUtil {

    /**
     *  放大縮小圖片
     * @param bitmap 要放大的圖片
     * @param dstWidth 目標寬
     * @param dstHeight 目標高
     * @return
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int dstWidth, int dstHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) dstWidth / width);
        float scaleHeight = ((float) dstHeight / height);
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                matrix, true);
        return newbmp;
    }

    /**
     * 將Drawable轉化為Bitmap
     * @param drawable
     * @return
     */
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;

    }

    /**
     * 獲得圓角圖片的方法
     * @param bitmap 
     * @param roundPx  4腳幅度
     * @return
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

    /**
     * 獲得帶倒影的圖片方法
     * @param bitmap
     * @return
     */
    public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
        final int reflectionGap = 4;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
                width, height / 2, matrix, false);

        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                (height + height / 2), Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bitmap, 0, 0, null);
        Paint deafalutPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
                0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                + reflectionGap, paint);

        return bitmapWithReflection;
    }

}


 

package com.nailsoul.imagedemo;

import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.nailsoul.imagedemo.utils.ImageUtil;

public class ImageDemoActivity extends Activity {
    private ImageView mIv01, mIv02,mIv03;
    private TextView mTv01, mtv02,mtv03,mtv00;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setFlags(flag,flag);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        setupViews();
    }

    private void setupViews() {
        mIv01 = findView(R.id.image01);
        mIv02 = findView(R.id.image02);
        mIv03 = findView(R.id.image03);
        mtv00=findView(R.id.text00);
        mTv01=findView(R.id.text01);
        mtv02=findView(R.id.text02);
        mtv03=findView(R.id.text03);

        // 獲取壁紙返回值是Drawable
//      Drawable drawable = getWallpaper();
        Drawable drawable=getResources().getDrawable(R.drawable.a1+new Random().nextInt(15));
        // 將Drawable轉化為Bitmap
        Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);
        // 縮放圖片
        Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 355, 317);
        // 獲取圓角圖片
        Bitmap roundBitmap = ImageUtil
                .getRoundedCornerBitmap(bitmap, 10.0f);
        // 獲取倒影圖片
        Bitmap reflectBitmap = ImageUtil
                .createReflectionImageWithOrigin(zoomBitmap);
        // 這里可以讓Bitmap再轉化為Drawable
        // Drawable roundDrawable = new BitmapDrawable(roundBitmap);
        // Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);
        // mImageView01.setBackgroundDrawable(roundDrawable);
        // mImageView02.setBackgroundDrawable(reflectDrawable);

        mIv01.setImageBitmap(roundBitmap);
        mIv02.setImageBitmap(reflectBitmap);
        mIv03.setImageBitmap(zoomBitmap);
        mtv00.setText("ImageUtils演示");
        mTv01.setText("上圖為圓角圖   width:"+roundBitmap.getWidth()+"height:"+roundBitmap.getHeight());
        mtv02.setText("上圖為倒影圖   width:"+reflectBitmap.getWidth()+"height:"+reflectBitmap.getHeight());
        mtv03.setText("上圖為縮放圖   width:"+zoomBitmap.getWidth()+"height:"+zoomBitmap.getHeight());
    }



    public <T> T findView(int id){
        return (T) super.findViewById(id);
    }
}


 

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/text00"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />

            <ImageView
                android:id="@+id/image01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10px" />

            <TextView
                android:id="@+id/text01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />

            <ImageView
                android:id="@+id/image02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10px" />

            <TextView
                android:id="@+id/text02"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />

            <ImageView
                android:id="@+id/image03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10px" />

            <TextView
                android:id="@+id/text03"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>


 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!