package com.img.util;

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

public class ImgUtil{
// 放大缩小图片
public static BitmapzoomBitmap(Bitmapbitmap, int w, int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrixmatrix
= new Matrix();
float scaleWidth = (( float )w / width);
float scaleHeight = (( float )h / height);
matrix.postScale(scaleWidth,scaleHeight);
BitmapnewBmp
= Bitmap.createBitmap(bitmap, 0 , 0 ,width,height,
matrix,
true );
return newBmp;
}

// 将Drawable转化为Bitmap
public static BitmapdrawableToBitmap(Drawabledrawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmapbitmap
= Bitmap.createBitmap(width,height,drawable
.getOpacity()
!= PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
:Bitmap.Config.RGB_565);
Canvascanvas
= new Canvas(bitmap);
drawable.setBounds(
0 , 0 ,width,height);
drawable.draw(canvas);
return bitmap;
}

// 圆角图片
public static BitmapgetRoundedCornerBitmap(Bitmapbitmap, float roundPx){
Bitmapoutput
= Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(),Config.ARGB_8888);
Canvascanvas
= new Canvas(output);

final int color = 0xff424242 ;
final Paintpaint = new Paint();
final Rectrect = new Rect( 0 , 0 ,bitmap.getWidth(),bitmap.getHeight());
final RectFrectF = 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;
}

// 获得带倒影的图片方法
public static BitmapcreateReflectionImageWithOrigin(Bitmapbitmap){
final int reflectionGap = 4 ;
int width = bitmap.getWidth();
int height = bitmap.getHeight();

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

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

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

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

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

Paintpaint
= new Paint();
LinearGradientshader
= new LinearGradient( 0 ,bitmap.getHeight(), 0 ,
bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff ,
0x00ffffff ,TileMode.CLAMP);
paint.setShader(shader);
// SettheTransfermodetobeporterduffanddestinationin
paint.setXfermode( new PorterDuffXfermode(Mode.DST_IN));
// Drawarectangleusingthepaintwithourlineargradient
canvas.drawRect( 0 ,height,width,bitmapWithReflection.getHeight()
+ reflectionGap,paint);

return bitmapWithReflection;
}
}

package com.img.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity{
private ImageViewmImageView01,mImageView02;

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

private void setupViews(){
mImageView01
= (ImageView)findViewById(R.id.image01);
mImageView02
= (ImageView)findViewById(R.id.image02);

// 获取壁纸返回值是Drawable
Drawabledrawable = getWallpaper();
// 将Drawable转化为Bitmap
Bitmapbitmap = ImgUtil.drawableToBitmap(drawable);
// 缩放图片
BitmapzoomBitmap = ImgUtil.zoomBitmap(bitmap, 100 , 100 );
// 获取圆角图片
BitmaproundBitmap = ImgUtil
.getRoundedCornerBitmap(zoomBitmap,
10.0f );
// 获取倒影图片
BitmapreflectBitmap = ImgUtil
.createReflectionImageWithOrigin(zoomBitmap);
// 这里可以让Bitmap再转化为Drawable
// DrawableroundDrawable=newBitmapDrawable(roundBitmap);
// DrawablereflectDrawable=newBitmapDrawable(reflectBitmap);
// mImageView01.setBackgroundDrawable(roundDrawable);
// mImageView02.setBackgroundDrawable(reflectDrawable);

mImageView01.setImageBitmap(roundBitmap);
mImageView02.setImageBitmap(reflectBitmap);
}
}

更多相关文章

  1. android剪切圆角图片的方法
  2. Android判断文件类型(视频、音频、图片等)
  3. android获取versionName和versionCode
  4. 获取Android(安卓)Device的信息
  5. Android手机开发:获取GPS信息
  6. Android如何从服务器获取图片
  7. Android:Gallery
  8. android直接从图片中对二维码编码解码
  9. Android如何获取asset目录下所有文件的路径

随机推荐

  1. android重复的文件复制APK META-INF许可
  2. 安卓布局知识点
  3. 特朗普打压微信,苹果不好受
  4. Android(安卓)SDK Platform 4.4.2【离线
  5. Android多线程--HandlerThread用法
  6. IntentTest
  7. 获取mic音量大小
  8. android在线播放mp4/3gp
  9. 一行代码引来的安全漏洞就让我们丢失了整
  10. android禁止状态栏下拉