Android图片倒影

来源:互联网 发布:公司网络管理需求分析 编辑:程序博客网 时间:2024/05/14 10:02

图片的倒影效果可以让想要显示的图片呈现出3D的效果,实现很简单,只需要将图片的下半部分翻转后加入线性渐变效果即可,可以实现如下图所示的效果:

这里首先创建一个用来图片处理的类:ReflectImage:

<span style="font-size:18px;">public class ReflectImage {private final static int REFLECT_GAP = 4;// 倒影区图像与原始图像之间的间距// 倒影图片创建类public static Bitmap createReflectImg(Bitmap oriBitmap) {int bitmapHeight = oriBitmap.getHeight() * 3 / 2 + REFLECT_GAP;int bitmapWidth = oriBitmap.getWidth() * 3 / 2;Bitmap reflectBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight,Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(reflectBitmap);canvas.drawBitmap(oriBitmap, 0, 0, null);Matrix ReflectM = new Matrix();ReflectM.preScale(1, -1);Bitmap newBitmap = Bitmap.createBitmap(oriBitmap, 0,oriBitmap.getHeight() / 2, oriBitmap.getWidth(),oriBitmap.getHeight() / 2, ReflectM, false);canvas.drawBitmap(newBitmap, 0, oriBitmap.getHeight() + REFLECT_GAP,null);Paint paint = new Paint();LinearGradient shader = new LinearGradient(0, oriBitmap.getHeight(), 0,bitmapHeight, 0x70ffffff, 0x00ffffff,TileMode.CLAMP);// Set the paint to use this shader (linear gradient)paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));canvas.drawRect(0, oriBitmap.getHeight(), oriBitmap.getWidth(),bitmapHeight, paint);return reflectBitmap;}}</span>

使用方法:

<span style="font-size:18px;">public class Main extends Activity{private ImageView imgShow;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 实例化launcher列表setContentView(R.layout.main);imgShow=(ImageView)findViewById(R.id.img_show);// MyImgView.drawableToBitmap(lvalue.get(position).icon)Bitmap bitmap = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.xuanjianyong);imgShow.setImageBitmap(ReflectImage.createReflectImg(bitmap));// 加入处理过的图片}}</span>




0 0