Gallery 横向滑动的刻度尺

Gallery(画廊)虽然已过时,官方也建议我们使用ViewPager,但是用它来做一下小功能还是可以的,
我们先来看效果图:

这里写图片描述

这里的有透明的效果,所以需要我们来自定义Gallery:

package com.example.hukaiguo1990.gallerydemo;

import android.content.Context;
import android.graphics.Matrix;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.RelativeLayout;

/**
 * 作者:hukaiguo1990
 * 利用Gallery实现横向刻度的滑动(Gallery 已经过期)
 */
public class CustomGallery extends Gallery{
    private float unselectedAlpha = 0.2f;
    public static final float SCALEDOWN_GRAVITY_CENTER = 0.75f;
    private float scaleDownGravity = SCALEDOWN_GRAVITY_CENTER;
    private float unselectedScale=0.5f;
        private Transformer mTransformer;

    public CustomGallery(Context context) {//View在代码中使用时被调用,布局中不调用
        super(context);
    }
    public CustomGallery(Context context, AttributeSet attrs) {//在布局使用时调用
        super(context, attrs);
    }
    public CustomGallery(Context context, AttributeSet attrs, int defStyleAttr) {//在布局使用时调用并指明Style
        super(context, attrs, defStyleAttr);
        if (!isInEditMode()) {//如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码,会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
                setStaticTransformationsEnabled(true);//启用静态变换,这样对于每个子视图都会调用getChildStaticTransformation()。
                setCallbackDuringFling(true);//设置Gallery在fling状态也可以选择
                //设置控件在被选中是的监听
                setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (mTransformer!=null) {
                            mTransformer.onItemSelected(parent, view, position, id);
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        }

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



        if (Build.VERSION.SDK_INT >= 16) {
            child.invalidate();
        }

        final int coverFlowWidth = getClientWidth();
        final int coverFlowCenter = coverFlowWidth / 2;
        final int childWidth = child.getWidth();
        final int childHeight = child.getHeight();
        final int childCenter = child.getLeft() + childWidth / 2;

        final int actionDistance = (int) ((coverFlowWidth + childWidth) / 2.0f);


        final float effectsAmount = Math.min(1.0f, Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - coverFlowCenter)));


        t.clear();
        t.setTransformationType(Transformation.TYPE_BOTH);


        if (this.unselectedAlpha != 1) {
            final float alphaAmount = (this.unselectedAlpha - 1) * Math.abs(effectsAmount) + 1;
            t.setAlpha(alphaAmount);
        }
        final Matrix imageMatrix = t.getMatrix();

        final float zoomAmount = (this.unselectedScale - 1) * Math.abs(effectsAmount) + 1;

        final float translateX = childWidth / 2.0f;
        final float translateY = childHeight * this.scaleDownGravity;
        imageMatrix.preTranslate(-translateX, -translateY);
        imageMatrix.postScale(zoomAmount, zoomAmount);
        imageMatrix.postTranslate(translateX, translateY);
        return true;
    }

    private int getClientWidth() {
        return getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    }


    public static interface Transformer {


        public void transformPage(View page, int count, int num);


        public void onItemSelected(AdapterView<?> parent, View view, int position, long id);
    }

    public  void setTransformer(Transformer transformer){
        mTransformer=transformer;
    }

}

activity_main.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="|"
        android:textColor="#FF0000"
        android:textSize="18sp"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <com.example.hukaiguo1990.gallerydemo.CustomGallery
        android:layout_width="match_parent"
        android:layout_below="@id/textView"
        android:layout_height="60dip"
        android:id="@+id/mGallery"
         />


</RelativeLayout>

item_gallery.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_shit"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="正常"
        android:textColor="#000000"
        android:textSize="40dp" />

</LinearLayout>

MainActivity.java

package com.example.hukaiguo1990.gallerydemo;

import android.content.Context;
import android.graphics.Matrix;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.RelativeLayout;

/**
 * 作者:hukaiguo1990
 * 利用Gallery实现横向刻度的滑动(Gallery 已经过期)
 */
public class CustomGallery extends Gallery{
    private float unselectedAlpha = 0.2f;
    public static final float SCALEDOWN_GRAVITY_CENTER = 0.75f;
    private float scaleDownGravity = SCALEDOWN_GRAVITY_CENTER;
    private float unselectedScale=0.5f;
        private Transformer mTransformer;

    public CustomGallery(Context context) {//View在代码中使用时被调用,布局中不调用
        super(context);
    }
    public CustomGallery(Context context, AttributeSet attrs) {//在布局使用时调用
        super(context, attrs);
    }
    public CustomGallery(Context context, AttributeSet attrs, int defStyleAttr) {//在布局使用时调用并指明Style
        super(context, attrs, defStyleAttr);
        if (!isInEditMode()) {//如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码,会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
                setStaticTransformationsEnabled(true);//启用静态变换,这样对于每个子视图都会调用getChildStaticTransformation()。
                setCallbackDuringFling(true);//设置Gallery在fling状态也可以选择
                //设置控件在被选中是的监听
                setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (mTransformer!=null) {
                            mTransformer.onItemSelected(parent, view, position, id);
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        }

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



        if (Build.VERSION.SDK_INT >= 16) {
            child.invalidate();
        }

        final int coverFlowWidth = getClientWidth();
        final int coverFlowCenter = coverFlowWidth / 2;
        final int childWidth = child.getWidth();
        final int childHeight = child.getHeight();
        final int childCenter = child.getLeft() + childWidth / 2;

        final int actionDistance = (int) ((coverFlowWidth + childWidth) / 2.0f);


        final float effectsAmount = Math.min(1.0f, Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - coverFlowCenter)));


        t.clear();
        t.setTransformationType(Transformation.TYPE_BOTH);


        if (this.unselectedAlpha != 1) {
            final float alphaAmount = (this.unselectedAlpha - 1) * Math.abs(effectsAmount) + 1;
            t.setAlpha(alphaAmount);
        }
        final Matrix imageMatrix = t.getMatrix();

        final float zoomAmount = (this.unselectedScale - 1) * Math.abs(effectsAmount) + 1;

        final float translateX = childWidth / 2.0f;
        final float translateY = childHeight * this.scaleDownGravity;
        imageMatrix.preTranslate(-translateX, -translateY);
        imageMatrix.postScale(zoomAmount, zoomAmount);
        imageMatrix.postTranslate(translateX, translateY);
        return true;
    }

    private int getClientWidth() {
        return getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    }


    public static interface Transformer {


        public void transformPage(View page, int count, int num);


        public void onItemSelected(AdapterView<?> parent, View view, int position, long id);
    }

    public  void setTransformer(Transformer transformer){
        mTransformer=transformer;
    }

}

源码:http://download.csdn.net/detail/hukaiguo/9543021

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值