第一步:新建一个Android工程命名为ImageDemo,工程结构如下:
第二步.java文件ImageUtil.java,在里面定义一些图片处理方法<span color:#333333;"="" style="font-size: 10.5pt; ">,代码如下:
001 | package com.android.tutor; |
002 | import android.graphics.Bitmap; |
003 | import android.graphics.Canvas; |
004 | import android.graphics.LinearGradient; |
005 | import android.graphics.Matrix; |
006 | import android.graphics.Paint; |
007 | import android.graphics.PixelFormat; |
008 | import android.graphics.PorterDuffXfermode; |
009 | import android.graphics.Rect; |
010 | import android.graphics.RectF; |
011 | import android.graphics.Bitmap.Config; |
012 | import android.graphics.PorterDuff.Mode; |
013 | import android.graphics.Shader.TileMode; |
014 | import android.graphics.drawable.Drawable; |
015 | public class ImageUtil { |
018 |
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){ |
019 |
int width = bitmap.getWidth(); |
020 |
int height = bitmap.getHeight(); |
021 |
Matrix matrix = new Matrix(); |
022 |
float scaleWidht = (( float )w / width); |
023 |
float scaleHeight = (( float )h / height); |
024 |
matrix.postScale(scaleWidht, scaleHeight); |
025 |
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0 , 0 , width, height, matrix, true ); |
029 |
public static Bitmap drawableToBitmap(Drawable drawable){ |
030 |
int width = drawable.getIntrinsicWidth(); |
031 |
int height = drawable.getIntrinsicHeight(); |
032 |
Bitmap bitmap = Bitmap.createBitmap(width, height, |
033 |
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 |
034 |
: Bitmap.Config.RGB_565); |
035 |
Canvas canvas = new Canvas(bitmap); |
036 |
drawable.setBounds( 0 , 0 ,width,height); |
037 |
drawable.draw(canvas); |
043 |
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx){ |
045 |
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap |
046 |
.getHeight(), Config.ARGB_8888); |
047 |
Canvas canvas = new Canvas(output); |
049 |
final int color = 0xff424242 ; |
050 |
final Paint paint = new Paint(); |
051 |
final Rect rect = new Rect( 0 , 0 , bitmap.getWidth(), bitmap.getHeight()); |
052 |
final RectF rectF = new RectF(rect); |
054 |
paint.setAntiAlias( true ); |
055 |
canvas.drawARGB( 0 , 0 , 0 , 0 ); |
056 |
paint.setColor(color); |
057 |
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); |
059 |
paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN)); |
060 |
canvas.drawBitmap(bitmap, rect, rect, paint); |
065 |
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ |
066 |
final int reflectionGap = 4 ; |
067 |
int width = bitmap.getWidth(); |
068 |
int height = bitmap.getHeight(); |
070 |
Matrix matrix = new Matrix(); |
071 |
matrix.preScale( 1 , - 1 ); |
073 |
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, |
074 |
0 , height/ 2 , width, height/ 2 , matrix, false ); |
076 |
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/ 2 ), Config.ARGB_8888); |
078 |
Canvas canvas = new Canvas(bitmapWithReflection); |
079 |
canvas.drawBitmap(bitmap, 0 , 0 , null ); |
080 |
Paint deafalutPaint = new Paint(); |
081 |
canvas.drawRect( 0 , height,width,height + reflectionGap, |
084 |
canvas.drawBitmap(reflectionImage, 0 , height + reflectionGap, null ); |
086 |
Paint paint = new Paint(); |
087 |
LinearGradient shader = new LinearGradient( 0 , |
088 |
bitmap.getHeight(), 0 , bitmapWithReflection.getHeight() |
089 |
+ reflectionGap, 0x70ffffff , 0x00ffffff , TileMode.CLAMP); |
090 |
paint.setShader(shader); |
092 |
paint.setXfermode( new PorterDuffXfermode(Mode.DST_IN)); |
094 |
canvas.drawRect( 0 , height, width, bitmapWithReflection.getHeight() |
095 |
+ reflectionGap, paint); |
097 |
return bitmapWithReflection; |
第三步:修改main.xml布局文件,主要放了两个ImageView控件,代码如下:
第四步:修改主核心程序,ImageDemo.java,代码如下:
01 | package com.android.tutor; |
02 | import android.app.Activity; |
03 | import android.graphics.Bitmap; |
04 | import android.graphics.drawable.Drawable; |
05 | import android.os.Bundle; |
06 | import android.widget.ImageView; |
07 | public class Imagedemo extends Activity { |
08 |
private ImageView mImageView01,mImageView02; |
10 |
public void onCreate(Bundle savedInstanceState) { |
11 |
super .onCreate(savedInstanceState); |
12 |
setContentView(R.layout.main); |
16 |
private void setupViews(){ |
17 |
mImageView01 = (ImageView)findViewById(R.id.image01); |
18 |
mImageView02 = (ImageView)findViewById(R.id.image02); |
21 |
Drawable drawable = getWallpaper(); |
23 |
Bitmap bitmap = ImageUtil.drawableToBitmap(drawable); |
25 |
Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100 , 100 ); |
27 |
Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10 .0f); |
29 |
Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(zoomBitmap); |
36 |
mImageView01.setImageBitmap(roundBitmap); |
37 |
mImageView02.setImageBitmap(reflectBitmap); |
第五步:运行上述工程,查看效果如下: