关闭

项目实战:引导动画-千变万化的ViewPager(文末小彩蛋)

153人阅读 评论(0) 收藏 举报
分类:

开发随笔,小结项目开发中的得与失,在项目中做了基于ViewPager十分酷炫的引导动画,在这里和大家分享一下:  

先在布局文件中写好ViewPager控件:

    <android.support.v4.view.ViewPager
        android:id="@+id/image_vp_guide"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    </android.support.v4.view.ViewPager>
做好初始化工作:

    private ViewPager image_vp_guide;
    private int mImgIds[] = new int[]{R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};
    private List<ImageView> mImages = new ArrayList<ImageView>();
    private static final String TAG = "GuideActivity";
```
    image_vp_guide = (ViewPager) findViewById(R.id.image_vp_guide);
接下来添加动画效果:

    image_vp_guide.setPageTransformer(true,new DepthPageTransformer());


package utils;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by 权兴权意 on 2016/8/31.
 */
public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}
设置适配器,重写相关方法,搞定啦:
        image_vp_guide.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mImgIds.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                ImageView imageView = new ImageView(GuideActivity.this);
                imageView.setImageResource(mImgIds[position]);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//保证图片不变形
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(GuideActivity.this,position,Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "instantiateItem: " + position);
                        if (position == mImgIds.length-1){
                            Intent i = new Intent();
                            i.setClass(GuideActivity.this, MainActivity.class);
                            startActivity(i);
                        }
                    }
                });

                container.addView(imageView);
                mImages.add(imageView);

                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImages.get(position));
            }
        });



可供参考:

自定义的viewpager的指示器

https://github.com/CTAS00/ViewPagerIndicator

ViewPager的滑动效果-Transformer

https://github.com/syusuke/ViewPagerAnim


Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

http://blog.csdn.net/lmj623565791/article/details/40411921/

巧用ViewPager 打造不一样的广告轮播切换效果

http://blog.csdn.net/lmj623565791/article/details/51339751

史上最简的ViewPagerIndicate,高仿网易新闻客户端效果

http://blog.csdn.net/u013519989/article/details/52315672



文末小彩蛋:

ctrl+shift+t:打开类


Activity去除标题栏和状态栏

在代码里设置:

//去除title   
requestWindowFeature(Window.FEATURE_NO_TITLE);   
//去掉Activity上面的状态栏
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);  

在配置文件里设置:

隐藏标题栏需要使用预定义样式:android:theme=”@android:style/Theme.NoTitleBar”.
隐藏状态栏:android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”.

在这里我还想说明一下,用前者在我们应用运行后,会看到短暂的状态栏,然后才全屏,而第二种方法是不会有这种情况的,所以我建议大家使用后者!


Android Studio如何删除module

选中你要移除的module,F12进入配置页,然后按红色的‘-’按钮,这个时候仅仅是把module从module列表中。

执行完这步操作后,这个时候你会发现app那个图标上的小手机不变了,delete就OK啦,

注意:这边delete按钮一定要慎重,删了就删了,硬盘上也同样被删了。



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78320次
    • 积分:2731
    • 等级:
    • 排名:第13229名
    • 原创:167篇
    • 转载:223篇
    • 译文:2篇
    • 评论:11条