当前位置:文档之家› 简单实现Android图片翻转动画效果

简单实现Android图片翻转动画效果

简单实现Android图片翻转动画效果
简单实现Android图片翻转动画效果

简单实现Android图片翻转动画效果不说废话,先看效果。

这是原始图片的样子

这是翻转后的效果图

如果是你想要的效果,那么继续往下看,如果不是,那可以跳过了。

这是一个动画,而不是用matrix实现的直接翻转图片。

可能网上有很多例子了,但我这是我自己实验过的,单纯的翻转一张图片。我这个是根据APIDemo简单修改写的

需要一个Rotate3d 类,继承Animation

public class Rotate3d extends Animation{

private final float mFromDegrees;

private final float mToDegrees;

private final float mCenterX;

private final float mCenterY;

private final float mDepthZ;

private final boolean mReverse;

private Camera mCamera;

public Rotate3d(float fromDegrees, float toDegrees,

float centerX, float centerY, float depthZ, boolean reverse) {

mFromDegrees = fromDegrees;

mToDegrees = toDegrees;

mCenterX = centerX;

mCenterY = centerY;

mDepthZ = depthZ;

mReverse = reverse;

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);

mCamera = new Camera();

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

final float fromDegrees = mFromDegrees;

float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

final float centerX = mCenterX;

final float centerY = mCenterY;

final Camera camera = mCamera;

final Matrix matrix = t.getMatrix();

camera.save();

if (mReverse) {

camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);

} else {

camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));

}

camera.rotateY(degrees);

camera.getMatrix(matrix);

camera.restore();

matrix.preTranslate(-centerX, -centerY);

matrix.postTranslate(centerX, centerY);

}

}

这个类可以直接拷过去,不用做任何的修改。其中的方法自己找相关资料研究。

在main.xml里加个ImageView,如

xmlns:android="https://www.doczj.com/doc/e93996040.html,/apk/res/android"

android:id="@+id/container"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rotate"

android:textSize="50px"

android:layout_x="150px"

android:layout_y="30px"

android:src="@drawable/ro">

>

这个不需要解释吧,都可以看懂的

最后,还需要一个activity类

如:

public class TestRotate extends Activity implements OnClickListener{

private mageView imageview;

private ViewGroup mContainer;

/**

*这个变量设置的是图片,如果是多张图片,那么可以用数组,如

*private static final int IMAGE = new int[]{

* R.drawable.ro,

* R.drawable.icon

*};

*有多少图片就放多少,我这里做的只是一张图片的翻转

*

*/

private static final int IMAGE = R.drawable.ro;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(https://www.doczj.com/doc/e93996040.html,yout.main);

imageview = (ImageView) findViewById(R.id.image);

mContainer = (ViewGroup) findViewById(R.id.container);

/**

* 设置最新显示的图片

* 如果是数组,那么可以写成IMAGE[int]

*

*/

imageview.setImageResource(IMAGE);

/**

*

* 设置ImageView的OnClickListener

*

*/

imageview.setClickable(true);

imageview.setFocusable(true);

imageview.setOnClickListener(this);

}

private void applyRotation(int position, float start,

float end) {

// Find the center of the container

final float centerX = mContainer.getWidth() / 2.0f;

final float centerY = mContainer.getHeight() / 2.0f;

final Rotate3d rotation =

new Rotate3d(start, end, centerX, centerY,

310.0f, true);

rotation.setDuration(500);

rotation.setFillAfter(true);

rotation.setInterpolator(new AccelerateInterpolator()); rotation.setAnimationListener(new

DisplayNextView(position));

mContainer.startAnimation(rotation);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

/**

*

* 调用这个方法,就是翻转图片

* 参数很简单,大家都应该看得懂

* 简单说下,第一个是位置,第二是开始的角度,第三个是结束的角度

* 这里需要说明的是,如果是要回到上一张

* 把第一个参数设置成-1就行了

*

*/

applyRotation(0,0,90);

}

private final class DisplayNextView implements

Animation.AnimationListener {

private final int mPosition;

private DisplayNextView(int position) {

mPosition = position;

}

public void onAnimationStart(Animation animation) {

}

public void onAnimationEnd(Animation animation) {

mContainer.post(new SwapViews(mPosition));

}

public void onAnimationRepeat(Animation animation) {

}

}

/**

* This class is responsible for swapping the views and

start the second

* half of the animation.

*/

private final class SwapViews implements Runnable {

private final int mPosition;

public SwapViews(int position) {

mPosition = position;

}

public void run() {

final float centerX = mContainer.getWidth() / 2.0f;

final float centerY = mContainer.getHeight() /

2.0f;

Rotate3d rotation;

if (mPosition > -1) {

imageview.setVisibility(View.VISIBLE);

imageview.requestFocus();

rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);

} else {

imageview.setVisibility(View.GONE);

rotation = new Rotate3d(90, 0, centerX,

centerY, 310.0f, false);

}

rotation.setDuration(500);

rotation.setFillAfter(true);

rotation.setInterpolator(new

DecelerateInterpolator());

mContainer.startAnimation(rotation);

}

}

}

代码里主要的地方我都注释了,大家自己看看,其他的都是不需要改,但也可根据自

己实际需要来修改

我这里比较适合一张图片的翻转,如果是多张图片,可以参考APIDemo里的例子,就

是加个ArrayAdapter,还是简单的,也可以自己发挥修改,实现自己想要的。

这里的代码基本上可以直接运行项目了。

供大家参考,希望大家实现出更多更强大的效果。

我刚研究这个动画2天,还很多不会,如有不足之处,欢迎大家提出来一起研究。

Android超炫图片浏览器代码

Android超炫图片浏览器代码 使用过Android自带的gallery组件的人都知道,gallery实现的效果就是拖动浏览一组图片,相比iphone里也是用于拖动浏览图片的coverflow,显然逊色不少。实际上,可以通过扩展gallery,通过伪3D变换可以基本实现coverflow的效果。本文通过源代码解析这一功能的实现。具体代码作用可参照注释。 最终实现效果如下: 要使用gallery,我们必须首先给其指定一个adapter。在这里,我们实现了一个自定义的ImageAdapter,为图片制作倒影效果。 传入参数为context和程序内drawable中的图片ID数组。之后调用其中的createReflectedImages()方法分别创造每一个图像的倒影效果,生成对应的ImageView数组,最后在getView()中返回。 /* * Copyright (C) 2010 Neil Davies * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.doczj.com/doc/e93996040.html,/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This code is base on the Android Gallery widget and was Created * by Neil Davies neild001 'at' gmail dot com to be a Coverflow widget * * @author Neil Davies */ public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds ;

Android图片处理工具类(圆角,压缩)

Android图片处理工具类(圆角,压缩) 工作中用到的图片处理工具类,简单写下来,以便备用! public class BitmapUtils { /** * 图像背景圆角处理 * bitmap要处理的图片 roundPx 图片弯角的圆度一般是5到10之间 */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { // 创建与原图大小一样的bitmap文件,Config.ARGB_8888根据情况可以改用其它的 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); // 实例画布,绘制的bitmap将保存至output中 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(android.graphics.PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); bitmap = null; return output; } /** * bitmap缩放 * width要缩放的宽度 height要缩放的高度 */ public static Bitmap getBitmapDeflation(Bitmap bitmap, int width, int height, boolean recycle) { if (null == bitmap) { return null;

android图片3d旋转

看到很多人在问如何实现三维的翻转效果,所以今天在这里简单的给大家分析一下,其实在APIDemo中就有这样一个例子,那么我们就以其为例来学习Android中的翻转动画效果的实现,首先看一下运行效果如下图所示。 Android中并没有提供直接做3D翻转的动画,所以关于3D翻转的动画效果需要我们自己实现,那么我们首先来分析一下Animation 和Transformation。 Animation动画的主要接口,其中主要定义了动画的一些属性比如开始时间,持续时间,是否重复播放等等。而Transformation中则包含一个矩阵和alpha值,矩阵是用来做平移,旋转和缩放动画的,而alpha值是用来做alpha动画的,要实现3D旋转动画我们需要继承自Animation类来实现,我们需要重载getTransformation和applyTransformation,在getTransformation中Animation会根据动画的属性来产生一系列的差值点,然后将这些差值点传给applyTransformation,这个函数将根据这些点来生成不同的Transformation。下面是具体实现: 1.public class Rotate3dAnimation extends Animation { 2.//开始角度 3. private final float mFromDegrees; 4.//结束角度 5. private final float mToDegrees; 6.//中心点

7. private final float mCenterX; 8. private final float mCenterY; 9. private final float mDepthZ; 10.//是否需要扭曲 11. private final boolean mReverse; 12.//摄像头 13. private Camera mCamera; 14. public Rotate3dAnimation(float fromDegrees, float toDegrees, 15. float centerX, float centerY, float depthZ, boolean reverse) { 16. mFromDegrees = fromDegrees; 17. mToDegrees = toDegrees; 18. mCenterX = centerX; 19. mCenterY = centerY; 20. mDepthZ = depthZ; 21. mReverse = reverse; 22. } 23. 24. @Override 25. public void initialize(int width, int height, int parentWidth, int par entHeight) { 26. super.initialize(width, height, parentWidth, parentHeight); 27. mCamera = new Camera(); 28. } 29.//生成Transformation 30. @Override 31. protected void applyTransformation(float interpolatedTime, Transformat ion t) { 32. final float fromDegrees = mFromDegrees; 33.//生成中间角度 34. float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interp olatedTime); 35. 36. final float centerX = mCenterX; 37. final float centerY = mCenterY; 38. final Camera camera = mCamera; 39. 40. final Matrix matrix = t.getMatrix(); 41. 42. camera.save(); 43. if (mReverse) { 44. camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); 45. } else {

Android 真正的3D图形(一)

Android 真正的3D图形(一) Design设计 在使用OpenGL 框架时一个好的设计原则是使用“Composite Pattern”,本篇采用如下设计: Mesh 首先定义一个基类Mesh,所有空间形体最基本的构成元素为Mesh(三角形网格),其基本定义如下: Java代码: Java代码 1publicclassMesh{ 2//Ourvertexbuffer. 3privateFloatBufferverticesBuffer=null; 4 5//Ourindexbuffer. 6privateShortBufferindicesBuffer=null; 7 8//Thenumberofindices. 9privateintnumOfIndices=-1; 10 11//FlatColor 12privatefloat[]rgba 13=newfloat[]{1.0f,1.0f,1.0f,1.0f}; 14 15//SmoothColors 16privateFloatBuffercolorBuffer=null; 17 18//Translateparams. 19publicfloatx=0; 20 21publicfloaty=0; 22 23publicfloatz=0; 24 25//Rotateparams. 26publicfloatrx=0; 27 28publicfloatry=0; 29 30publicfloatrz=0; 31

32publicvoiddraw(GL10gl){ 33//Counter-clockwisewinding. 34gl.glFrontFace(GL10.GL_CCW); 35//Enablefaceculling. 36gl.glEnable(GL10.GL_CULL_FACE); 37//Whatfacestoremovewiththefaceculling. 38gl.glCullFace(GL10.GL_BACK); 39//Enabledtheverticesbufferforwritingand 40//tobeusedduring 41//rendering. 42gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 43//Specifiesthelocationanddataformat 44//ofanarrayofvertex 45//coordinatestousewhenrendering. 46gl.glV ertexPointer(3,GL10.GL_FLOA T,0,verticesBuffer); 47//Setflatcolor 48gl.glColor4f(rgba[0],rgba[1],rgba[2],rgba[3]); 49//Smoothcolor 50if(colorBuffer!=null){ 51//Enablethecolorarraybuffertobe 52//usedduringrendering. 53gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 54gl.glColorPointer(4,GL10.GL_FLOA T,0,colorBuffer); 55} 56 57gl.glTranslatef(x,y,z); 58gl.glRotatef(rx,1,0,0); 59gl.glRotatef(ry,0,1,0); 60gl.glRotatef(rz,0,0,1); 61 62//Pointoutthewherethecolorbufferis. 63gl.glDrawElements(GL10.GL_TRIANGLES,numOfIndices, 64GL10.GL_UNSIGNED_SHORT,indicesBuffer); 65//Disabletheverticesbuffer. 66gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 67//Disablefaceculling. 68gl.glDisable(GL10.GL_CULL_FACE); 69} 70 71protectedvoidsetV ertices(float[]vertices){ 72//afloatis4bytes,therefore 73//wemultiplythenumberif 74//verticeswith4. 75ByteBuffervbb

安卓图片批量处理软件 (12页)

本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除! == 本文为word格式,下载后可方便编辑和修改! == 安卓图片批量处理软件 篇一:Android_解决图片大量下载:软引用 Android 解决图片大量下载:软引用 1.对象的强、软、弱和虚引用 为了能更加灵活控制对象的生命周期,需要知道对象引用的4中级别,由高到低依次为:强引用、软引用、弱引用和虚引用 备注:这四种的区别: ⑴强引用(StrongReference) 强引用是使用最普遍的引用。如果一个对象具有强引用,那垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题。 ⑵软引用(SoftReference) 如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存(下文给出示例)。软引用可以和一个引用队列(ReferenceQueue)联合使用,如果软引用所引用的对象被垃圾回收器回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。 ⑶弱引用(WeakReference) 弱引用与软引用的区别在于:只具有弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程,因此不一定会很快发现那些只具有弱引用的对象。弱引用可以和一个引用队列(ReferenceQueue)联合使用,如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。

Android 图片加载性能优化总结

Android 图片加载性能优化总结 一、Android Bitmap加载大尺寸图片优化: 压缩原因: 1.imageview大小如果是200*300那么加载个2000*3000的图片到内存中显然是浪费可耻滴行为; 2.最重要的是图片过大时直接加载原图会造成OOM异常(out of memory内存溢出) 所以一般对于大图我们需要进行下压缩处理 看不懂英文的话木有关系,本篇会有介绍 主要处理思路是: 1.获取图片的像素宽高(不加载图片至内存中,所以不会占用资源) 2.计算需要压缩的比例 3.按将图片用计算出的比例压缩,并加载至内存中使用 官网大图片加载教程(上面网址里的)对应代码就是: /** * 获取压缩后的图片 * @param res * @param resId * @param reqWidth 所需图片压缩尺寸最小宽度 * @param reqHeight 所需图片压缩尺寸最小高度 * @return */ public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

// 首先不加载图片,仅获取图片尺寸 final BitmapFactory.Options options = new BitmapFactory.Options(); // 当inJustDecodeBounds设为true时,不会加载图片仅获取图片尺寸信息 options.inJustDecodeBounds = true; // 此时仅会将图片信息会保存至options对象内,decode方法不会返回bitmap 对象 BitmapFactory.decodeResource(res, resId, options); // 计算压缩比例,如inSampleSize=4时,图片会压缩成原图的1/4 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 当inJustDecodeBounds设为false时,BitmapFactory.decode...就会返回图片对象了 options.inJustDecodeBounds = false; // 利用计算的比例值获取压缩后的图片对象 return BitmapFactory.decodeResource(res, resId, options); } 代码详解: 核心方法是BitmapFactory.decode...(...., options) ...的意思是此外还有一系列的decodeFile/decodeStream等等方法,都是利用options灵活解析获取图片, 只不过解析图片的来源不同罢了,比如网络图片获取,一般就是解析字节流信息然后decode获取图片实例 Options是图片配置信息,参数详细介绍下: inJustDecodeBounds 是否只解析边界 设为true时去decode获取图片,只会加载像素宽高信息 设为false时decode则会完全加载图片 inSampleSize 压缩比例

图片的旋转和缩放android高级

view plaincopy to clipboardprint? 1.package com.test.activity; 2. 3.import android.app.Activity; 4.import android.graphics.Bitmap; 5.import android.graphics.BitmapFactory; 6.import android.graphics.Matrix; 7.import android.graphics.drawable.BitmapDrawable; 8.import android.os.Bundle; 9.import https://www.doczj.com/doc/e93996040.html,youtParams; 10. import android.widget.ImageView; 11. import android.widget.LinearLayout; 12. import android.widget.ImageView.ScaleType; 13. 14. public class MainActivity extends Activity { 15. public void onCreate(Bundle icicle) { 16. super.onCreate(icicle); 17. LinearLayout linLayout = new LinearLayout(this); 18. // 加载需要操作的图片,这里是eoeAndroid的logo图片 19. Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 20. R.drawable.sss); 21. 22. //获取这个图片的宽和高 23. int width = bitmapOrg.getWidth(); 24. int height = bitmapOrg.getHeight(); 25. 26. //定义预转换成的图片的宽度和高度 27. int newWidth = 200; 28. int newHeight = 200;

Android图片处理(Matrix,ColorMatrix)

在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老照片的效果,有时候还要对图片进行变换,以拉伸,扭曲等等。 这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。 下面将分别介绍这两个矩阵的用法和相关的函数。 颜色矩阵 android中可以通过颜色矩阵(ColorMatrix类)方面的操作颜色,颜色矩阵是一个5x4 的矩阵(如图1.1) 可以用来方面的修改图片中RGBA各分量的值,颜色矩阵以一维数组的方式存储如下: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ] 他通过RGBA四个通道来直接操作对应颜色,如果会使用Photoshop就会知道有时处理图片通过控制RGBA各颜色通道来做出特殊的效果。 这个矩阵对颜色的作用计算方式如1.3示: 矩阵的运算规则是矩阵A的一行乘以矩阵C的一列作为矩阵R的一行, C矩阵是图片中包含的ARGB信息,R矩阵是用颜色矩阵应用于C之后的新的颜色分量,运算结果如下: R' = a*R + b*G + c*B + d*A + e; G' = f*R + g*G + h*B + i*A + j; B' = k*R + l*G + m*B + n*A + o; A' = p*R + q*G + r*B + s*A + t; 颜色矩阵并不是看上去那么深奥,其实需要使用的参数很少,而且很有规律第一行决定红色第二行决定绿色

第三行决定蓝色,第四行决定了透明度,第五列是颜色的偏移量。下面是一个实际中使用的颜色矩阵。 如果把这个矩阵作用于各颜色分量的话,R=A*C,计算后会发现,各个颜色分量实际上没有任何的改变(R'=R G'=G B'=B A'=A)。 图1.5所示矩阵计算后会发现红色分量增加100,绿色分量增加100, 这样的效果就是图片偏黄,因为红色和绿色混合后得到黄色,黄色增加了100,图片当然就偏黄了。 改变各颜色分量不仅可以通过修改第5列的颜色偏移量也可如上面矩阵所示将对应的颜色值乘以一个倍数,直接放大。 上图1.6是将绿色分量乘以2变为原来的2倍。相信读者至此已经明白了如何通过颜色矩阵来改变各颜色分量。 下面编写一段代码来,通过调整颜色矩阵来获得不同的颜色效果,JavaCode如下: 复制到剪贴板 Java代码 1 CMatrix类: 2public class CMatrix extends Activity { 3 4private Button change; 5private EditText [] et=new EditText[20];

【黑马程序员】安卓中选择本地图片或拍照获取图片并裁剪...

【黑马程序员】安卓中选择本地图片或拍照获取图片并裁剪 在开发的时候我们经常遇到一种需求,就是让用户上传一张图片作为头像。这个图片可能是从相册中选择的,也可能是使用摄像头现拍的。不过得到的图片都会进行一步裁剪的操作。下面我们就来实现一下这个需求。 首先我们定义一个Activity ,布局里只放置一个按钮和一个ImageView ,按钮点击的时候让用户选择并裁剪图片,ImageView 作为最终显示结果的View 对象。布局代码如下: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17

相关文档 最新文档