美文网首页
画廊效果

画廊效果

作者: 成旭辕 | 来源:发表于2017-12-31 21:23 被阅读0次

这两天研究画廊效果,在github 上发现一个还不错的开源项目,集成了一下,下面来看下效果


1514724610614mz22.gif

下面看下集成过程
首先在build.gradle中导入相应的依赖包

compile 'com.github.moondroid.coverflow:library:1.0'

新建一个GameEntity实体类

public class GameEntity {
    public int imageResId;
    public int titleResId;
    public GameEntity (int imageResId, int titleResId){
        this.imageResId = imageResId;
        this.titleResId = titleResId;
    }
}

适配器类

package com.coverflow;

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

import java.util.ArrayList;

/**
 * 作者: chengcheng
 * 时间: 17/12/29- 上午10:10
 * 描述:
 */

public class CoverFlowAdapter extends BaseAdapter {

    private ArrayList<GameEntity> mData = new ArrayList<>(0);
    private Context mContext;

    public CoverFlowAdapter(Context context) {
        mContext = context;
    }

    public void setData(ArrayList<GameEntity> data) {
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int pos) {
        return mData.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.item_coverflow, null);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.label);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.image);
            rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.image.setImageResource(mData.get(position).imageResId);
        holder.text.setText(mData.get(position).titleResId);
        holder.image.setTag(mData.get(position));

        return rowView;
    }


    static class ViewHolder {
        public TextView text;
        public ImageView image;
    }
}

MainActivity类,在这个类里面处理数据

package com.coverflow;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

import it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow;

public class MainActivity extends AppCompatActivity {

    private CoverFlowAdapter mAdapter;
    private ArrayList<GameEntity> mData = new ArrayList<>(0);
    private TextSwitcher mTitle;
    private FeatureCoverFlow mCoverFlow;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.jump);
        mTitle = (TextSwitcher) findViewById(R.id.title);
        mCoverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);
        initData();
        setOtherClick();
    }

    private void initData() {
        mData.add(new GameEntity(R.drawable.image_1, R.string.title1));
        mData.add(new GameEntity(R.drawable.image_2, R.string.title2));
        mData.add(new GameEntity(R.drawable.image_3, R.string.title3));
        mData.add(new GameEntity(R.drawable.image_4, R.string.title4));
        mData.add(new GameEntity(R.drawable.image_5, R.string.title5));
        mData.add(new GameEntity(R.drawable.image_6, R.string.title6));
        mData.add(new GameEntity(R.drawable.image_7, R.string.title7));
        mData.add(new GameEntity(R.drawable.image_8, R.string.title8));
        mData.add(new GameEntity(R.drawable.image_9, R.string.title9));

        mAdapter = new CoverFlowAdapter(this);
        mAdapter.setData(mData);
        mCoverFlow.setAdapter(mAdapter);
        setCoverFlowOnItemClick();
    }

    private void setCoverFlowOnItemClick() {
        mCoverFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ImageView imageView = view.findViewById(R.id.image);
                if (imageView != null) {
                    GameEntity entity = (GameEntity) imageView.getTag();
                    Toast.makeText(MainActivity.this, entity.titleResId, Toast.LENGTH_SHORT).show();
                }
            }
        });

        mCoverFlow.setOnScrollPositionListener(new FeatureCoverFlow.OnScrollPositionListener() {
            @Override
            public void onScrolledToPosition(int position) {
                mTitle.setText(getResources().getString(mData.get(position).titleResId));
            }

            @Override
            public void onScrolling() {
                mTitle.setText("");
            }
        });
    }

    private void setOtherClick() {
        mTitle.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                TextView textView = (TextView) inflater.inflate(R.layout.item_title, null);
                return textView;
            }
        });
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }


}

activity_main.xml 界面布局

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

  
    <it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow
        android:id="@+id/coverflow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        coverflow:coverHeight="@dimen/cover_height"
        coverflow:coverWidth="@dimen/cover_width"
        coverflow:maxScaleFactor="1.6"
        coverflow:reflectionGap="0px"
        coverflow:reflectionHeight="0.5"
        coverflow:rotationThreshold="0.5"
        coverflow:scalingThreshold="0.5"
        coverflow:spacing="0.5"
        />

    <TextSwitcher
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:paddingBottom="26dp"/>

    <TextView
        android:id="@+id/jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:padding="12dp"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="15sp"
        android:text="跳转"/>

</RelativeLayout>

item_coverflow.xml 适配器布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="@dimen/cover_width"
             android:layout_height="@dimen/cover_height"
             android:clickable="true"
             android:foreground="@drawable/cover_selector">


    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        tools:src="@drawable/image_1"/>

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/label_background"
        android:gravity="center"
        android:padding="8dp"
        android:textAppearance="?android:attr/textAppearanceSmallInverse"
        android:visibility="gone"
        tools:text="Title"/>
</FrameLayout>

item_title.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:textColor="@color/red"
          android:textSize="13sp"
          tools:text="title"/>

cover_selector.xml 选中阴影效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="#96000000" />
    </item>
    <item>
        <color android:color="@android:color/transparent" />
    </item>
</selector>

代码其实很简单,文章结尾部分是我在GitHub上面的Demo,可以下载看下

注意下一些属性的使用
coverWidth 图片的宽度
coverHeight 图片的高度
scalingThreshold 距离中心的距离是小部件大小的一半,在这里,它开始缩放
1意味着从小部件的边缘开始缩放,0意味着只有中心缩放默认是0.5
adjustPositionMultiplier 调整扩大图片的间距 默认1.0f
maxScaleFactor 选中图片放大倍速 1.5倍合适
reflectionGap  反射和原始图像在像素上的差距
rotationThreshold 旋转角度
spacing 图片之间的间隔
circlePathRadius 圆径的半径。屏幕的范围是-1到1,最小半径是1,默认是2f
reflectionHeight 倒影的高度 默认是0.5
reflectionBackroundColor 倒影的背景色 默认是透明
alignAnimationTime 对齐动画需要多长时间 默认是350毫秒

在github上的地址
https://github.com/shaozucheng/MyCoverFlowDemo
需要的小伙伴最好去下载代码看下

原本的github 地址:https://github.com/moondroid/CoverFlow

相关文章

  • 画廊效果

    这两天研究画廊效果,在github 上发现一个还不错的开源项目,集成了一下,下面来看下效果 下面看下集成过程首先在...

  • CollectionView画廊效果

    以下代码直接添加到自定义的FlowLayout中即可

  • ViewPager切换动画

    1. 3D画廊效果 2.相互连接显示效果 3.水滴指示器效果 4.动态设置是否可滚动效果 1. 3D画廊效果 关键...

  • 有用的库

    画廊效果implementation'github.hellocsl:GalleryLayoutManager:1...

  • collectionView 实现画廊效果

    效果 标题是collectionView,但其实这篇文章说的是自定义FlowLayout的事? 大家都知道coll...

  • 安卓画廊效果

    1:效果图 2:代码 2:创建activity代码 3:xml代码 4:适配器 你要还不会,就加我微信,我去你家教你

  • 实用功能 - 收藏集 - 掘金

    使用 RecyclerView 实现 Gallery 画廊效果,并控制 Item 停留位置 - 掘金Recycle...

  • 实用功能 - 收藏集 - 掘金

    使用 RecyclerView 实现 Gallery 画廊效果,并控制 Item 停留位置 - 掘金Recycle...

  • 点击图片显示画廊效果

    看起来很复杂,实际上只是用到了v-show,v-show可以控制组件显示与否。其实项目不是最终目的,而是通过项目去...

  • 用js与css3实现画廊页面效果

    基于前一篇爬虫爬取的素材,完成一个酷炫的画廊效果页面 最终效果图: 效果演示效果预览.gif 页面布局: 页面样式...

网友评论

      本文标题:画廊效果

      本文链接:https://www.haomeiwen.com/subject/tilwgxtx.html