当前位置:操作系统 > 安卓/Android >>

Android--Gallery实例

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);
}

}

www.zzzyk.com
<?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.draw

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,