Android--Gallery实例

来源:互联网 发布:无线网卡修改mac地址 编辑:程序博客网 时间:2024/04/28 08:54
1)新建项目 Gallery04
2)导入图片
3)编写ImageAdapter 
4)编写GalleryFlow
5)编写layout_gallery.xml

6)编写Gallery04Activity





import android.content.Context;
import android.content.res.Resources;
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.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;


public class ImageAdapter extends BaseAdapter {


int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds;
private ImageView[] mImages;


public ImageAdapter(Context c, Integer[] ImageIds) {
mContext = c;
mImageIds = ImageIds;
mImages = new ImageView[mImageIds.length];
}


public boolean createReflectedImages() {
final int reflectionGap = 4;
int index = 0;


for (int imageId : mImageIds) {
Bitmap originalImage = BitmapFactory.decodeResource(mContext
.getResources(), imageId);
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 bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);


Canvas canvas = new Canvas(bitmapWithReflection);


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


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


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


Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalImage
.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);


paint.setShader(shader);


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


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


ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);
imageView.setLayoutParams(new GalleryFlow.LayoutParams(180, 240));
// imageView.setScaleType(ScaleType.MATRIX);
mImages[index++] = imageView;
}
return true;
}


private Resources getResources() {
// TODO Auto-generated method stub
return null;
}


public int getCount() {
return mImageIds.length;
}


public Object getItem(int position) {
return position;
}


public long getItemId(int position) {
return position;
}


public View getView(int position, View convertView, ViewGroup parent) {
return mImages[position];
}


public float getScale(boolean focused, int offset) {
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}


}



package com.kingsoft.gallery04;


import android.content.Context;
import android.graphics.Camera;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Gallery;


public class GalleryFlow extends Gallery {

private Camera mCamera = new Camera();
private int mMaxRotationAngle = 60;
private int mMaxZoom = -120;
private int mCoveflowCenter;


public GalleryFlow(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setStaticTransformationsEnabled(true);
}

public GalleryFlow(Context context,AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
this.setStaticTransformationsEnabled(true);
}

public GalleryFlow(Context context,AttributeSet attrs,int defStyle) {
super(context,attrs,defStyle);
// TODO Auto-generated constructor stub
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;
}

    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
                        + getPaddingLeft();
}


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

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCoveflowCenter = getCenterOfCoverflow();
        super.onSizeChanged(w, h, oldw, oldh);
}

}



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >


<com.kingsoft.gallery04.GalleryFlow 
android:id="@+id/Gallery04" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true"/>
</RelativeLayout>



package com.kingsoft.gallery04;


import android.app.Activity;
import android.os.Bundle;


public class Gallery04Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gallery);
        
        Integer[] images = { R.drawable.img0001, R.drawable.img0030,
                R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
                R.drawable.img0230, R.drawable.img0300, R.drawable.img0330,
                R.drawable.img0354 };
        
        ImageAdapter adapter = new ImageAdapter(this,images);
//        adapter.createReflectionImages();
        adapter.createReflectedImages();
        GalleryFlow galleryFlow = (GalleryFlow)findViewById(R.id.Gallery04);
        galleryFlow.setAdapter(adapter);
        
        
    }
}


运行即可。



下面修改一下,添加动画效果:

anim.xml


<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
<scale
android:interpolator= "@android:anim/accelerate_decelerate_interpolator"
      android:fromXScale="0.5"
android:toXScale="1"
android:fromYScale="0.5"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:fillBefore="true"
android:fillAfter="true"
android:duration="1000"
>
   
      </scale>
</set>  

修改Gallery04Activity


package com.kingsoft.gallery04;


import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;


public class Gallery04Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gallery);
        
        Integer[] images = { R.drawable.img0001, R.drawable.img0030,
                R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
                R.drawable.img0230, R.drawable.img0300, R.drawable.img0330,
                R.drawable.img0354 };
        
        final Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.layout.anim);
        final AnimationSet animationSet = new AnimationSet(true);
        
        ImageAdapter adapter = new ImageAdapter(this,images);
//        adapter.createReflectionImages();
        adapter.createReflectedImages();
        GalleryFlow galleryFlow = (GalleryFlow)findViewById(R.id.Gallery04);
        galleryFlow.setAdapter(adapter);
        
        /*
         * 经过屏幕中间触发动画
         */
//        galleryFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
//
// @Override
// public void onItemSelected(AdapterView<?> arg0, View arg1,
// int arg2, long arg3) {
// // TODO Auto-generated method stub
// Gallery04Activity.this.setTitle(String.valueOf(arg2));
// arg1.startAnimation(anim);
//
//
// }
//
// @Override
// public void onNothingSelected(AdapterView<?> arg0) {
// // TODO Auto-generated method stub
//
// }
// });
        
        
        /**
         * 点击选项触发动画
         */
//        galleryFlow.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
// long arg3) {
// // TODO Auto-generated method stub
// Gallery04Activity.this.setTitle(String.valueOf(arg3));
// arg1.startAnimation(anim);
//
// }
// });
        
        
        
        /**
         * 不能触发动画效果
         */
//        galleryFlow.setOnFocusChangeListener(new OnFocusChangeListener() {
//
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// // TODO Auto-generated method stub
// Gallery04Activity.this.setTitle(String.valueOf(v.getId()));
// v.startAnimation(anim);
//
// }
// });
        


        /**
         * 不适用,作用于整个gallery而不是单个Item
         */
//        galleryFlow.setOnTouchListener(new OnTouchListener() {
//
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// // TODO Auto-generated method stub
// Gallery04Activity.this.setTitle(String.valueOf(v.getId()));
// v.startAnimation(anim);
// return true;
// }
// });
        
        
        galleryFlow.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
arg0.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Gallery04Activity.this.setTitle(String.valueOf(arg2));
arg1.startAnimation(anim);
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
}



});
        
        
        /**
         * 无明显作用
         */
//        galleryFlow.setOnItemLongClickListener(new OnItemLongClickListener() {
//
// @Override
// public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
// int arg2, long arg3) {
// // TODO Auto-generated method stub
// return false;
// }
// });
    }
}














原创粉丝点击