您的位置:首页 > 其它

ImageUtils工具演示

2013-10-19 04:55 274 查看
该工具提供缩放 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>






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: