首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

ImageView倒影成效

2012-08-11 
ImageView倒影效果ImageView倒影效果的两种方法第一种:写个图片的工具类,在Activity中用java代码调用Image

ImageView倒影效果
ImageView倒影效果的两种方法

第一种:
写个图片的工具类,在Activity中用java代码调用ImageView组件的setImageBitmap(Bitmap bitmap)方法。
下面是图片工具类:

import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;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 ImageUtils {/**   * 从SDCard上读取图片   * @param pathName   * @return */public static Bitmap getBitmapFromSDCard(String pathName) {return BitmapFactory.decodeFile(pathName);}/**   * 缩放图片   * @param bitmap   * @param width   * @param height   * @return */public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {int w = bitmap.getWidth();int h = bitmap.getHeight();Matrix matrix = new Matrix();matrix.postScale((float) width / w, (float) height / h);return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);}/**   * 将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   * @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 Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);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 getReflectionImageWithOrigin(Bitmap bitmap) {// 原始图片和反射图片中间的间距final int reflectionGap = 0;int width = bitmap.getWidth();int height = bitmap.getHeight();// 反转  Matrix matrix = new Matrix();// 第一个参数为1表示x方向上以原比例为准保持不变,正数表示方向不变。           // 第二个参数为-1表示y方向上以原比例为准保持不变,负数表示方向取反。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 defaultPaint = new Paint();canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);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);paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);return bitmapWithReflection;}}


在Activity中这样调用,先获取XML文件中定义的Imageview对象
image = (ImageView) findViewById(R.id.movie);

然后将获取到的Imageview对象转化为Bitmap类型
Bitmap imageBit = ((BitmapDrawable) image.getDrawable()).getBitmap();

接着调用图片工具类中的方法,返回一个Bitmap对象,将这个Bitmap对象放置在ImageView中
Bitmap m = ImageUtils.getReflectionImageWithOrigin(imageBit);image.setImageBitmap(m);



第二种
继承ImageView类
import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuffXfermode;import android.graphics.Bitmap.Config;import android.graphics.PorterDuff.Mode;import android.graphics.Shader.TileMode;import android.graphics.drawable.BitmapDrawable;import android.util.AttributeSet;import android.widget.ImageView;public class ReflectImageView extends ImageView {private Bitmap originalBitmap;public ReflectImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ReflectImageView(Context context) {this(context, null, 0);}public ReflectImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);DoReflection(((BitmapDrawable) getDrawable()).getBitmap());}@Overridepublic void setImageBitmap(Bitmap bm) {DoReflection(bm);}/** 显示倒影效果的setImageBitmap函数 */public void setImageBitmap(Bitmap bm, boolean isFlected) {if (isFlected) {super.setImageBitmap(bm);}}@Overridepublic void setImageResource(int resId) {originalBitmap = BitmapFactory.decodeResource(getResources(), resId);DoReflection(originalBitmap);}private void DoReflection(Bitmap bitmap) {final int reflectionGap = 0;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 bitmap4Reflection = Bitmap.createBitmap(width,(height + height / 2), Config.ARGB_8888);Canvas canvasRef = new Canvas(bitmap4Reflection);Paint deafaultPaint = new Paint();deafaultPaint.setAntiAlias(true);canvasRef.drawBitmap(bitmap, 0, 0, null);canvasRef.drawRect(0, height, width, height + reflectionGap,deafaultPaint);canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null);Paint paint = new Paint();LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,bitmap4Reflection.getHeight() + reflectionGap, 0x70ffffff,0x00ffffff, TileMode.CLAMP);paint.setShader(shader);paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));canvasRef.drawRect(0, height, width, bitmap4Reflection.getHeight()+ reflectionGap, paint);this.setImageBitmap(bitmap4Reflection, true);}}

然后在xml文件里面配置
<com.hello.ReflectImageView        android:id="@+id/imageView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@+drawable/button1" />

其中
com.hello.ReflectImageView
是继承类的完整包名

热点排行