Android开发3D界面特效



Android开发3D界面特效
001
package com.test.util;
002

003
import com.dooya.activity.R;
004

005
import android.content.Context;
006
import android.graphics.Bitmap;
007
import android.graphics.BitmapFactory;
008
import android.graphics.Canvas;
009
import android.graphics.LinearGradient;
010
import android.graphics.Matrix;
011
import android.graphics.Paint;
012
import android.graphics.PorterDuffXfermode;
013
import android.graphics.Bitmap.Config;
014
import android.graphics.PorterDuff.Mode;
015
import android.graphics.Shader.TileMode;
016
import android.graphics.drawable.BitmapDrawable;
017
import android.view.View;
018
import android.view.ViewGroup;
019
import android.widget.BaseAdapter;
020
import android.widget.ImageView;
021

022
public class ImageAdapter extends BaseAdapter {
023
    int mGalleryItemBackground;
024
    private Context mContext;
025
    //加载资源图片
026
    private Integer[] mImageIds = {
027
            R.drawable.favorite,
028
            R.drawable.room,
029
            R.drawable.scene,
030
            R.drawable.security,
031
            R.drawable.time,
032
            R.drawable.set};
033

034
    public ImageAdapter(Context c) {
035
        mContext = c;
036
    }
037

038
    public int getCount() {
039
        return mImageIds.length;
040
    }
041

042
    public Object getItem(int position) {
043
        return position;
044
    }
045

046
    public long getItemId(int position) {
047
        return position;
048
    }
049

050
    public View getView(int position, View convertView, ViewGroup parent) {
051

052
        ImageView i = createReflectedImages(mContext,mImageIds[position]);
053
        
054
        i.setLayoutParams(new CoverFlow.LayoutParams(120, 100));
055
        i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
056
        
057
        // 设置的抗锯齿
058
        BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
059
        drawable.setAntiAlias(true);
060
        return i;
061
    }
062

063
    public float getScale(boolean focused, int offset) {
064
        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
065
    }
066
    
067
    /**
068
     * 设置镜像图像
069
     * @param mContext
070
     * @param imageId
071
     * <a href="http://my.oschina.net/u/556800" rel="nofollow" target="_blank">@return</a>
072
     */
073
    public ImageView createReflectedImages(Context mContext,int imageId) {
074

075
        Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
076
        
077
        final int reflectionGap = 4;
078
        
079
        int width = originalImage.getWidth();
080
        int height = originalImage.getHeight();
081

082
        Matrix matrix = new Matrix();
083
        matrix.preScale(1, -1);
084

085
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
086
                height / 2, width, height / 2, matrix, false);
087

088
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
089
                (height + height / 2), Config.ARGB_8888);
090

091
        Canvas canvas = new Canvas(bitmapWithReflection);
092

093
        canvas.drawBitmap(originalImage, 0, 0, null);
094

095
        Paint deafaultPaint = new Paint();
096
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
097

098
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
099

100
        Paint paint = new Paint();
101
        LinearGradient shader = new LinearGradient(0, originalImage
102
                .getHeight(), 0, bitmapWithReflection.getHeight()
103
                + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
104

105
        paint.setShader(shader);
106

107
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
108

109
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
110
                + reflectionGap, paint);
111

112
        ImageView imageView = new ImageView(mContext);
113
        imageView.setImageBitmap(bitmapWithReflection);
114

115
        return imageView;
116
    }
117
    
118
}
001
package com.test.util;
002

003
import android.content.Context;
004
import android.graphics.Camera;
005
import android.graphics.Matrix;
006
import android.util.AttributeSet;
007
import android.view.View;
008
import android.view.animation.Transformation;
009
import android.widget.Gallery;
010
import android.widget.ImageView;
011

012
public class CoverFlow extends Gallery {
013

014
    private Camera mCamera = new Camera();
015
    private int mMaxRotationAngle = 50;//60;
016
    private int mMaxZoom = -380;//-120;
017
    private int mCoveflowCenter;
018
    private boolean mAlphaMode = true;
019
    private boolean mCircleMode = false;
020

021
    public CoverFlow(Context context) {
022
        super(context);
023
        this.setStaticTransformationsEnabled(true);
024
    }
025

026
    public CoverFlow(Context context, AttributeSet attrs) {
027
        super(context, attrs);
028
        this.setStaticTransformationsEnabled(true);
029
    }
030

031
    public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
032
        super(context, attrs, defStyle);
033
        this.setStaticTransformationsEnabled(true);
034
    }
035

036
    public int getMaxRotationAngle() {
037
        return mMaxRotationAngle;
038
    }
039

040
    public void setMaxRotationAngle(int maxRotationAngle) {
041
        mMaxRotationAngle = maxRotationAngle;
042
    }
043

044
    public boolean getCircleMode() {
045
        return mCircleMode;
046
    }
047

048
    public void setCircleMode(boolean isCircle) {
049
        mCircleMode = isCircle;
050
    }
051

052
    public boolean getAlphaMode() {
053
        return mAlphaMode;
054
    }
055

056
    public void setAlphaMode(boolean isAlpha) {
057
        mAlphaMode = isAlpha;
058
    }
059

060
    public int getMaxZoom() {
061
        return mMaxZoom;
062
    }
063

064
    public void setMaxZoom(int maxZoom) {
065
        mMaxZoom = maxZoom;
066
    }
067

068
    private int getCenterOfCoverflow() {
069
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
070
                + getPaddingLeft();
071
    }
072

073
    private static int getCenterOfView(View view) {
074
        return view.getLeft() + view.getWidth() / 2;
075
    }
076

077
    protected boolean getChildStaticTransformation(View child, Transformation t) {
078
        final int childCenter = getCenterOfView(child);
079
        final int childWidth = child.getWidth();
080
        int rotationAngle = 0;
081
        t.clear();
082
        t.setTransformationType(Transformation.TYPE_MATRIX);
083
        if (childCenter == mCoveflowCenter) {
084
            transformImageBitmap((ImageView) child, t, 0);
085
        } else {
086
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
087
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
088
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
089
                        : mMaxRotationAngle;
090
            }
091
            transformImageBitmap((ImageView) child, t, rotationAngle);
092
        }
093
        return true;
094
    }
095

096
    /**
097
     *
098
     */
099
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
100
        mCoveflowCenter = getCenterOfCoverflow();
101
        super.onSizeChanged(w, h, oldw, oldh);
102
    }
103

104
    /**
105
     * 把图像位图的角度通过
106
     */
107
    private void transformImageBitmap(ImageView child, Transformation t,
108
            int rotationAngle) {
109
        mCamera.save();
110
        final Matrix imageMatrix = t.getMatrix();
111
        final int imageHeight = child.getLayoutParams().height;
112
        final int imageWidth = child.getLayoutParams().width;
113
        final int rotation = Math.abs(rotationAngle);
114
        mCamera.translate(0.0f, 0.0f, 100.0f);
115

116
        // 如视图的角度更少,放大
117
        if (rotation <= mMaxRotationAngle) {
118
            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
119
            mCamera.translate(0.0f, 0.0f, zoomAmount);
120
            if (mCircleMode) {
121
                if (rotation < 40)
122
                    mCamera.translate(0.0f, 155, 0.0f);
123
                else
124
                    mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);
125
            }
126
            if (mAlphaMode) {
127
                ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));
128
            }
129
        }
130
        mCamera.rotateY(rotationAngle);
131
        mCamera.getMatrix(imageMatrix);
132
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
133
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
134
        mCamera.restore();
135
    }
136
}
01
package com.test.activity;
02

03
import android.app.Activity;
04

05
import android.os.Bundle;
06

07
import android.view.WindowManager;
08

09
import com.dooya.util.CoverFlow;
10

11
import com.dooya.util.ImageAdapter;
12

13
 
14

15
public class HomeActivity extends Activity {
16

17
    public void onCreate(Bundle savedInstanceState) {
18

19
        super.onCreate(savedInstanceState);
20

21
        CoverFlow cf = new CoverFlow(this);
22

23
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
24

25
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
26

27
        cf.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg));//背景
28

29
        cf.setAdapter(new ImageAdapter(this));
30

31
        ImageAdapter imageAdapter = new ImageAdapter(this);
32

33
        cf.setAdapter(imageAdapter);
34

35
        cf.setSelection(2, true);
36

37
        cf.setAnimationDuration(1000);
38

39
        setContentView(cf);
40

41
    }
42

43
}

你可能感兴趣的:(android)