Android 轻松实现图片倒影效果实例代码
主activity
package com.mj.myweather;
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.widget.imageview;
import com.mj.myweather.utils.imageutil;
public class imageactivity extends activity {
// 声明控件
private imageview mimageview01, mimageview02;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.image);
setupviews();
}
private void setupviews() {
mimageview01 = (imageview) findviewbyid(r.id.image01);
mimageview02 = (imageview) findviewbyid(r.id.image02);
// 获取壁纸返回值是drawable
drawable drawable = getwallpaper();
// 将drawable转化为bitmap
bitmap bitmap = imageutil.drawabletobitmap(drawable);
// 缩放图片
bitmap zoombitmap = imageutil.zoombitmap(bitmap, 300, 300);
// 获取圆角图片
bitmap roundbitmap = imageutil
.getroundedcornerbitmap(zoombitmap, 10.0f);
// 获取倒影图片
bitmap reflectbitmap = imageutil
.createreflectionimagewithorigin(roundbitmap);
// 这里可以让bitmap再转化为drawable
// drawable rounddrawable = new bitmapdrawable(roundbitmap);
// drawable reflectdrawable = new bitmapdrawable(reflectbitmap);
// mimageview01.setbackgrounddrawable(rounddrawable);
// mimageview02.setbackgrounddrawable(reflectdrawable);
mimageview01.setimagebitmap(roundbitmap);
mimageview02.setimagebitmap(reflectbitmap);
}
}
布局文件
<?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" >
<imageview
android:id="@+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
<imageview
android:id="@+id/image02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</linearlayout>
图片处理
package com.mj.myweather.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 w
* @param h
* @return
*/
public static bitmap zoombitmap(bitmap bitmap, int w, int h) {
int width = bitmap.getwidth();
int height = bitmap.getheight();
matrix matrix = new matrix();
float scalewidht = ((float) w / width);
float scaleheight = ((float) h / 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;
}
/*
* 获得圆角图片的方法
*/
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 originalimage
* @return
*/
public static bitmap createreflectionimagewithorigin(bitmap originalimage) {
final int reflectiongap = 4;
int width = originalimage.getwidth();
int height = originalimage.getheight();
matrix matrix = new matrix();
// 图片矩阵变换(从低部向顶部的倒影)// 实现图片翻转90度
matrix.prescale(1, -1);
// 创建反转后的图片bitmap对象,图片高是原图的一半
bitmap reflectionimage = bitmap.createbitmap(originalimage, 0, height / 2,
width, height / 2, matrix, false);
// 创建标准的bitmap对象,宽和原图一致,高是原图的1.5倍
bitmap bitmapwithreflection = bitmap.createbitmap(width,
(height + height / 2), config.argb_8888);
// 创建画布对象,将原图画于画布,起点是原点位置
canvas canvas = new canvas(bitmapwithreflection);
canvas.drawbitmap(originalimage, 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对象
lineargradient shader = new lineargradient(0, originalimage.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;
}
}
推荐阅读
-
Android图片实现压缩处理的实例代码
-
Android 实现ViewPager边界回弹效果实例代码
-
Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍
-
Android自定义ScrollView实现放大回弹效果实例代码
-
Android自定义view实现太极效果实例代码
-
Android 实现抖音头像底部弹框效果的实例代码
-
android BottomSheetDialog新控件解析实现知乎评论列表效果(实例代码)
-
Android 轻松实现图片倒影效果实例代码
-
android显示TextView文字的倒影效果实现代码
-
Android重写ImageView实现图片镜像效果的代码教程