【安卓笔记】切换图片(底部带有小点效果)

下面我们要实现这样的效果:

我们将采用两种方式实现这种效果:

1.使用ViewPager:

思路:ViewPager提供左右滑动图片操作的支持,下方小点在代码中动态创建,整个布局采用FrameLayout。

先看布局:

<FrameLayout 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"
    android:background="@android:color/black"
    tools:context=".MainActivity" >
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></android.support.v4.view.ViewPager>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/viewGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            ></LinearLayout>
    </RelativeLayout>
</FrameLayout>

再看代码:

package com.example.splash_viewpager;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnPageChangeListener
{
    private ViewPager mViewPager = null;
    private LinearLayout mViewGroup = null;

    private int[] mImageIds = {R.drawable.bg1,R.drawable.bg2,
            R.drawable.bg3,R.drawable.bg4,
            R.drawable.bg5,R.drawable.bg6,
            R.drawable.bg7};

    private ImageView[] mImageViews = null;

    private ImageView[] mTips = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);

        mTips = new ImageView[mImageIds.length];
        //动态创建小点并加到布局中
        for(int i = 0; i < mTips.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new LayoutParams(10,10));
            mTips[i] = iv;

            if(i == 0)
            {
                iv.setBackgroundResource(R.drawable.page_indicator_focused);
            }else
            {
                iv.setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            mViewGroup.addView(iv,lp);
        }
        mImageViews = new ImageView[mImageIds.length];
        for(int i = 0; i < mImageViews.length; i++)
        {
            ImageView iv = new ImageView(this);
            mImageViews[i] = iv;
            int reqWidth = getWindowManager().getDefaultDisplay().getWidth();
            int reqHeight = getWindowManager().getDefaultDisplay().getHeight();
            iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mImageIds[i], reqWidth, reqHeight));
        }

        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.setOnPageChangeListener(this);

    }

    class MyPagerAdapter extends PagerAdapter
    {
        @Override
        public int getCount()
        {
            return mImageIds.length;
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == arg1;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            try
            {
                container.addView(mImageViews[position]);
            } catch (Exception e)
            {
            }
            return mImageViews[position];
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
        }
    }
    @Override
    public void onPageSelected(int arg0)
    {
        for(int i = 0; i < mTips.length; i++)
        {
            if(arg0 == i)
            {
                mTips[i].setBackgroundResource(R.drawable.page_indicator_focused);
            }else
            {
                mTips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }
    @Override
    public void onPageScrollStateChanged(int arg0)
    {
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
    }

    private static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight)
    {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId);
        int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res,resId,opts);
    }

    private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)
    {
        if(opts == null)
        {
            return 1;
        }
        int inSampleSize = 1;
        int realWidth = opts.outWidth;
        int realHeight = opts.outHeight;

        if(realWidth > width || realHeight > height)
        {
            int heightRatio = realHeight/height;
            int widthRatio = realWidth/width;

            inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }
}

2.使用ViewFlipper

思路:在ViewFlipper中填充多个ImageView,然后使用GestureDetector检测手势,小点仍然是通过动态产生。

布局:

<FrameLayout 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"
    android:background="@android:color/black"
    tools:context=".MainActivity" >

    <ViewFlipper
        android:id="@+id/vf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></ViewFlipper>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/viewGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            ></LinearLayout>
    </RelativeLayout>
</FrameLayout>

代码:

package com.example.splash_viewflipper;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends Activity implements OnGestureListener
{
    private ViewFlipper mViewFlipper = null;
    private LinearLayout mViewGroup = null;

    private GestureDetector mGestureDetector = null;

    private int[] mImageIds = {
            R.drawable.bg1,R.drawable.bg2,
            R.drawable.bg3,R.drawable.bg4,
            R.drawable.bg5,R.drawable.bg6,
            R.drawable.bg7
    };

    private ImageView[] mImageViews = null;
    private ImageView[] mTips = null;

    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewFlipper = (ViewFlipper) findViewById(R.id.vf);
        mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);

        mGestureDetector = new GestureDetector(this,this);

        mImageViews = new ImageView[mImageIds.length];
        for(int i = 0; i < mImageViews.length; i++)
        {
            ImageView iv = new ImageView(this);
            int reqWidth = getWindowManager().getDefaultDisplay().getWidth();
            int reqHeight = getWindowManager().getDefaultDisplay().getHeight();
            iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(),mImageIds[i], reqWidth, reqHeight));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            mViewFlipper.addView(iv,lp);
        }

        mTips = new ImageView[mImageIds.length];
        for(int i = 0; i < mTips.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new LayoutParams(10,10));
            mTips[i] = iv;

            if(i == 0)
            {
                iv.setBackgroundResource(R.drawable.page_indicator_focused);
            }else
            {
                iv.setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            mViewGroup.addView(iv,lp);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        return mGestureDetector.onTouchEvent(event);
    }

    private static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight)
    {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId);
        int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res,resId,opts);
    }

    private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)
    {
        if(opts == null)
        {
            return 1;
        }
        int inSampleSize = 1;
        int realWidth = opts.outWidth;
        int realHeight = opts.outHeight;

        if(realWidth > width || realHeight > height)
        {
            int heightRatio = realHeight/height;
            int widthRatio = realWidth/width;

            inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY)
    {
        if(e1.getX() - e2.getX() > 120)//显示下一张
        {
            if(currentIndex != mTips.length-1)
            {
                currentIndex++;
                setImageBackground(currentIndex);
                Animation in_right = AnimationUtils.loadAnimation(this,R.anim.in_right_);
                Animation out_left = AnimationUtils.loadAnimation(this,R.anim.out_left_);
                mViewFlipper.setInAnimation(in_right);
                mViewFlipper.setOutAnimation(out_left);
                mViewFlipper.showNext();
            }else
            {
                Toast.makeText(this,"已经是最后一张..",0).show();
            }
        }else if(e1.getX() - e2.getX() < -120)//显示上一张
        {
            if(currentIndex != 0)
            {
                currentIndex--;
                setImageBackground(currentIndex);
                Animation in_left = AnimationUtils.loadAnimation(this,R.anim.in_left_);
                Animation out_right = AnimationUtils.loadAnimation(this,R.anim.out_right_);
                mViewFlipper.setInAnimation(in_left);
                mViewFlipper.setOutAnimation(out_right);
                mViewFlipper.showPrevious();
            }else
            {
                Toast.makeText(this,"已经是第一张..",0).show();
            }
        }
        return true;
    }
    private void setImageBackground(int selectItems)
    {
        for(int i = 0; i < mTips.length; i++)
        {
            if(i == selectItems)
            {
                mTips[i].setBackgroundResource(R.drawable.page_indicator_focused);
            }else
            {
                mTips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }
    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }
    @Override
    public void onShowPress(MotionEvent e)
    {
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY)
    {
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e)
    {
    }
}

【安卓笔记】切换图片(底部带有小点效果)

时间: 2024-10-21 12:53:03

【安卓笔记】切换图片(底部带有小点效果)的相关文章

【安卓笔记】仿猎豹清理大师波浪效果

先来看效果: 实现方式----->自定义控件 核心代码: package com.example.wavedemo1; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Handler; import

jQuery箭头切换图片 - 学习笔记

jQuery箭头切换图片 布局 3d位移 变形原点 jQuery transform:translate3d(x,y,z):        x 代表横向坐标移向量的长度       y 代表纵向坐标移向量的长度       z 代表Z轴移向量的长度 取值不可为百 scale() 缩放 transform-origin:0 50%:        top left | left top 等价于 0 0       top | top center | center top 等价于 50% 0   

android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

首先我们还是看一些示例:(网易,新浪,百度) 显示效果都不错,可是手感就不一样了,百度最棒,网易还行,新浪就操作很不好,这里我说的是滑动切换图片.自己可以测试一下.不得不说牛叉的公司确实有哦牛叉的道理. 下面我简单的介绍下实现方法:其实就是listview addHeaderView.只不过这个view是一个可以切换图片的view,至于这个view怎么做,就要根据自己的喜爱了,实现有多种方法,下面我简单介绍一下. 第一种:ViewFlipper+GestureDetector 主布局就是一个li

Android笔记之 图片自由裁剪

前言--项目中需要用到对用户头像的裁剪和上传功能.关于裁剪,一开始是想自己来做,但是觉得这个东西应该谷歌有开发吧,于是一搜索官方文档,果然有.于是,就果断无耻地用了Android自带有关于照片的自由裁剪.因为时间太紧,虽然不太华丽,但是胜在能用,节省时间嘛. 具体是通过 Intent的action来实现的. 关键代码如下: public void imageCut(Uri uri) { Intent intent = new Intent("com.android.camera.action.C

图片轮播小插件

在手上工作完成之余,自己写了一个jQuery小插件,针对我们现在所用到的图片轮播特效进行一个封装,没事就写写,记录我的前端历程上的点点滴滴. 编写背景: 在web端经常会看到图片滚来滚去的,自己就想一个方式,让前端团队开发这些特效时间更快,效果更好,实现特效的方式更多,可以随意替换图片轮播的方式,兼容各个浏览器,是我的初衷,也是对自己的js编程经验一个积攒,我相信带着兴趣做自己喜欢的事,是工作生活的初衷意义... 插件介绍: 本“小插”是基于jQuery上进行开发,对所播放的图片大小做了自适应,

【转】Android android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

首先我们还是看一些示例:(网易,新浪,百度)      下面我简单的介绍下实现方法:其实就是listview addHeaderView.只不过这个view是一个可以切换图片的view,至于这个view怎么做,就要根据自己的喜爱了,实现有多种方法,下面我简单介绍一下. 第一种:ViewFlipper+GestureDetector 主布局就是一个listview,这里就不介绍了,我介绍下切换图片布局head_iamge.xml 1 <span style="font-size:12px;&

Core Animation一些Demo总结 (动态切换图片、大转盘、图片折叠、进度条等动画效果)

前一篇总结了Core Animation的一些基础知识,这一篇主要是Core Animation 的一些应用,涉及到CAShapeLayer.CAReplicatorLayer等图层的知识. 先看效果图: 1.切换图片:      2.彩票转盘 3.图片折叠 4.进度条旋转 5.粒子效果 一.切换图片 看起来很复杂的动画,通过少量的计算和编码就可以简单的实现.要做到这一步,必须是需要研究iOS开发中的Core Animation和Core Graphics框架的.日常工作中,对于很多东西不求甚解

【安卓笔记】快速开发设置界面-----PreferenceActivity

通常app都会有一个设置界面,如下: 通常做法是自己定义布局,然后在代码里面添加响应函数,并将结果保存到Sharedpreferences中. android给我们提供了PreferenceActivity来简化开发设置界面. 你只需这样做: 1.创建一个类继承PreferenceActivity,并导入设置界面布局: package com.example.preferenceactivitydemo1; import android.os.Bundle; import android.pre

css通用小笔记03——浏览器窗口变小 div错位的问题

我最近写网页的时候,经常碰到一个普遍的问题,经过我的查阅和尝试,终于解决了这一问题,这里有两种方法提供给大家,如果博友还有更好的方法,欢迎补充. 一.使用min-width属性: 我们先看看下面这段代码(html): 1 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 2 <head> 3 <title>无标题文档</