凡客诚品 商品详情中 图片循环

凡客诚品 商品详情中 图片循环_第1张图片

按照 凡客诚品 Android客户端的商品详情中,商品显示方式来做的(http://m.vancl.com/m/android.html)


MainActivity.java

package com.xzq.gallerytest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private MyGallery gallery;
    private ImageAdapter adapter;
    private ImageView pagePoint;

    public static DisplayMetrics displayMetrics;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        displayMetrics = getDisplayMetrics(this);
        setContentView(R.layout.main);
        pagePoint = (ImageView) findViewById(R.id.pagePoint);
        gallery = (MyGallery) findViewById(R.id.gallery);
        adapter = new ImageAdapter(this);
        gallery.setAdapter(adapter); // gallery添加ImageAdapter图片资源
        gallery.setSpacing((int) displayMetrics.density); // 设置图片之间的间距

        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 绘制红点
                Bitmap bitmap = drawPoint(7, position % 7, MainActivity.this, R.drawable.dot_normal,
                        R.drawable.dot_selected, 12);
                pagePoint.setImageBitmap(bitmap);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    public static Bitmap drawPoint(int num, int position, Context context, int a, int b, int size) {

        Bitmap normal = ((BitmapDrawable) context.getResources().getDrawable(a)).getBitmap();
        Bitmap select = ((BitmapDrawable) context.getResources().getDrawable(b)).getBitmap();
        Bitmap bitmap = Bitmap.createBitmap((int) (num * size * displayMetrics.density),
                (int) (size * displayMetrics.density), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        int x = 0;
        for (int i = 0; i < num; i++) {
            if (i == position) {
                canvas.drawBitmap(select, x * displayMetrics.density, 0, null);
            } else {
                canvas.drawBitmap(normal, x * displayMetrics.density, 0, null);
            }
            x += size;
        }
        return bitmap;

    }

    /**
     * 获取屏幕管理类
     * 
     * @return DisplayMetrics 屏幕管理对象
     */
    public static DisplayMetrics getDisplayMetrics(Activity context) {
        DisplayMetrics displayMetrics = null;
        if (displayMetrics == null) {
            displayMetrics = new DisplayMetrics();
        }
        context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }
}

MyGallery.java

package com.xzq.gallerytest;

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;

public class MyGallery extends Gallery {

    private Camera mCamera = new Camera(); // 用户图片缩放摄像机
    private int mCoveflowCenter; // 控件中心
    private float moveWidth = 80 * MainActivity.displayMetrics.density;
    // 滑动过程中图片变化宽度
    private float moveHeigth = 150 * MainActivity.displayMetrics.density;

    /**
     * 构造方法
     * 
     * @param context
     *            上下文
     */
    public MyGallery(Context context) {
        super(context);
        // this.setStaticTransformationsEnabled(true);

    }

    /**
     * 构造方法
     * 
     * @param context
     *            上下文
     * @param attrs
     *            属性
     */
    public MyGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        // this.setStaticTransformationsEnabled(true);
    }

    /**
     * 构造方法
     * 
     * @param context
     *            上下文
     * @param attrs
     *            属性
     * @param defStyle
     *            样式
     */
    public MyGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // this.setStaticTransformationsEnabled(true);
    }

    // 获取控件中心位置
    private int getCenterOfCoverflow() {
        return getWidth() / 2;
    }

    // 获取每个imageview的中心位置
    private int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {

        final int childCenter = getCenterOfView(child); // 子view的中心
        int size = 0; // 变化大小

        t.clear(); // 清空移动
        t.setTransformationType(Transformation.TYPE_MATRIX); // 设置移动类型

        // 若子view的中心等于控件的中心则不作任何处理
        if (childCenter == mCoveflowCenter) {
            return true;
        }

        // 子view与中心得距离
        size = (mCoveflowCenter - childCenter); // * mMaxSize / (mMaxSize + 20);
        // 变成正直
        size = (size < 0) ? -size : size;
        // 动态改变子view大小
        transformImageBitmap(child, t, size);
        return true;
    }

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

    /**
     * 动态改变子view的大小
     * 
     * @param child
     *            当前view
     * @param t
     *            位移变化
     * @param size
     *            大小(距离中心的位置)
     */
    private void transformImageBitmap(View child, Transformation t, int size) {
        mCamera.save();
        final Matrix imageMatrix = t.getMatrix();
        final int sizeChange = Math.abs(size);
        mCamera.translate(0.0f, 0.0f, sizeChange);
        mCamera.getMatrix(imageMatrix);
        imageMatrix.preTranslate(-moveWidth, -moveHeigth);
        imageMatrix.postTranslate(moveWidth, moveHeigth);
        mCamera.restore();
    }
}

ImageAdapter.java

package com.xzq.gallerytest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;

    // 图片数组源
    public Integer[] imgs = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6 };

    public ImageAdapter(Context c) {
        context = c;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return imgs.length;
    }

    // 获取图片位置
    @Override
    public Object getItem(int position) {
        return imgs[position];
    }

    // 获取图片ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获得要显示的数据集中指定位置的数据元素的View
     * 
     * @param position
     *            我们想要获得的元素在适配器数据集中的位置
     * @param convertView
     *            可再重用的View
     * @param parent
     *            当前View所附属的父对象
     * @return 指定位置的数据元素的View
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        HodlerView hodlerView = null;
        if (convertView == null) {
            hodlerView = new HodlerView();
            convertView = inflater.inflate(R.layout.item, parent, false);
            hodlerView.imgProduct = (ImageView) convertView.findViewById(R.id.imgProduct);
        } else {
            hodlerView = (HodlerView) convertView.getTag();
        }
        convertView.setTag(hodlerView);
        hodlerView.imgProduct.setImageResource(imgs[position]);
        return convertView;
    }

    public static class HodlerView {
        public ImageView imgProduct; // 产品图片
    }
}



总体的代码我传上去了 凡客诚品 商品详情中 图片循环  http://download.csdn.net/detail/cmdkmd/4326071








你可能感兴趣的:(null,Integer,Class,float,Matrix,transformation)