实现单张图片的倒影效果
来源:互联网 发布:linux vnc服务器 编辑:程序博客网 时间:2024/06/10 02:03
本文章主要参考来自于:http://blog.csdn.net/zd_1471278687/article/details/13998413(谢谢分享)
在自己的需求上稍作修改:
1、图片的显示需要一个MyGallery类,
以下是MyGallery的.java
import android.content.Context;import android.graphics.Camera;import android.graphics.Matrix;import android.util.AttributeSet;import android.view.View;import android.view.animation.Transformation;import android.widget.Gallery;import android.widget.ImageView;/** * Created by Administrator on 2016/4/22. */public class MyGallery extends Gallery { private Camera mCamera = new Camera(); private int mMaxRotationAngle = 60; //图片偏转角度 60 private int mMaxZoom = -120; private int mCoveflowCenter; public MyGallery(Context context) { super(context); this.setStaticTransformationsEnabled(true); } public MyGallery(Context context, AttributeSet attrs) { super(context, attrs); this.setStaticTransformationsEnabled(true); } public MyGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setStaticTransformationsEnabled(true); } public int getMaxRotationAngle() { return mMaxRotationAngle; } public void setMaxRotationAngle(int maxRotationAngle) { mMaxRotationAngle = maxRotationAngle; } public int getMaxZoom() { return mMaxZoom; } public void setMaxZoom(int maxZoom) { mMaxZoom = maxZoom; } /** 获得Gallery中心到边界的距离*/ private int getCenterOfCoverflow() { return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft(); } /** 获得View中心位置到边界的距离 */ private static int getCenterOfView(View view) { return view.getLeft() + view.getWidth() / 2; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mCoveflowCenter = getCenterOfCoverflow(); super.onSizeChanged(w, h, oldw, oldh); } @Override protected boolean getChildStaticTransformation(View child, Transformation trans) { final int childCenter = getCenterOfView(child); final int childWidth = child.getWidth(); int rotationAngle = 0; trans.clear(); trans.setTransformationType(Transformation.TYPE_BOTH); // alpha和 matrix都变换 if (childCenter == mCoveflowCenter) { //正中间的childView transformImageBitmap((ImageView) child, trans, 0); } else { //两侧的childView rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle ); if (Math.abs(rotationAngle) > mMaxRotationAngle) { rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle; } transformImageBitmap((ImageView) child, trans, rotationAngle); } return true; } private void transformImageBitmap(ImageView child, Transformation trans, int rotationAngle) { mCamera.save(); final Matrix imageMatrix = trans.getMatrix(); final int imageHeight = child.getLayoutParams().height; final int imageWidth = child.getLayoutParams().width; final int rotation = Math.abs(rotationAngle); //在Z轴上正向移动camera的视角,实际效果为放大图片; 如果在Y轴上移动,则图片上下移动; X轴上对应图片左右移动 mCamera.translate(0.0f, 0.0f, 200.0f); // As the angle of the view gets less, zoom in if (rotation < mMaxRotationAngle) { float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); mCamera.translate(0.0f, 0.0f, zoomAmount); } mCamera.rotateY(rotationAngle); //rotationAngle 为正,沿y轴向内旋转; 为负,沿y轴向外旋转 mCamera.getMatrix(imageMatrix); imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); mCamera.restore(); }}2、在MainActivity中的使用: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.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Shader;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView image; Integer imgss = R.drawable.img2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getpic(); } private void getpic() { final int reflectionGap = 4;//原图与倒影之间的间隙 int index = 0; Bitmap originalImage = BitmapFactory.decodeResource( this.getResources(), imgss); // 获得图片资源 // 获得图片的长宽 int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); // 实现图片的反转 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); // 创建反转后的图片Bitmap对象,图片高是原图的一半 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888); // 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍 Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(originalImage, 0, 0, null); // 创建画布对象,将原图画于画布,起点是原点位置 Paint paint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, paint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // 将反转后的图片画到画布中 paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR);// 创建线性渐变LinearGradient对象 paint.setShader(shader); // 绘制 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//倒影遮罩效果 canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); // 画布画出反转图片大小区域,然后把渐变效果加到其中,就出现了图片的倒影效果 ImageView imageView = new ImageView(this); imageView.setImageBitmap(bitmapWithReflection); // 设置带倒影的Bitmap //设置ImageView的大小,可以根据图片大小设置 imageView.setLayoutParams(new MyGallery.LayoutParams(width, height)); // imageView.setLayoutParams(new FancyCoverFlow.LayoutParams(250, 500));//设置ImageView的大小,可根据需要设置固定宽高 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//将图片按比例缩放 image = (ImageView) findViewById(R.id.imageview); image.setImageDrawable(imageView.getDrawable()); }}3、xml文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.lql.anew.gallery3d.MainActivity"> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>4、最后效果:
0 0
- 实现单张图片的倒影效果
- 实现图片倒影效果
- 实现图片的倒影镜面效果
- Android实现图片的倒影效果
- Android实现图片的倒影效果
- Android实现图片的倒影效果分析
- Android:实现图片的倒影效果
- Android实现图片的倒影效果
- Android实现图片倒影效果
- IOS实现图片倒影效果
- Android 实现图片倒影效果
- Android实现图片倒影效果
- IOS实现图片倒影效果
- Android 实现图片倒影效果
- 图片倒影的实现
- 一个Android实现图片的倒影效果的代码示例
- 很好的一篇文章,如何实现图片倒影的效果
- Android平台上实现图片的缩放,圆角,倒影效果
- windows下vs2010 编译live555源码库的环境搭建
- CSS文档规范
- 苹果原生框架分享 ----- Social
- android RSA加密的使用
- NSAssert()
- 实现单张图片的倒影效果
- 343. Integer Break
- FileReader搭配FileWriter,更新对抛出的机制做回应
- poj2594
- 基于JavaScript代码去掉H5页面中的头尾及广告部分(支持 Android 和 iOS)
- Unity3D的几种坐标系,以及屏幕解锁类似功能
- hdu1978——How many ways(记忆化dp)
- apache commons fileupload 问题跟踪系统
- js url上添加随机数防止缓存