快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

黄金城棋牌游戏网址:用ViewPager实现高仿图片左右滑动自动切换的优酷Android客户端



本例是用ViewPager去做的实现,支持自动滑动和手动滑动黄金城棋牌游戏网址,不仅优酷网,实际上有很多商城和门户网站都有类似的实现:

详细思路:

1. 工程中必要添加android-support-v4.jar,才能应用ViewPager控件.

2. 图片的自动切换: 可应用Timer或者ScheduledExecutorService,这个有多重要领可以实现.

同时要切换底部的dots(园点)

3.Handler+Message机制更新UI,这个信托大年夜家都很纯熟,不再描述

4. 实现的一些细节:留意本例中的优化:图片的自动切换启动了其他的线程,要在Activity在可见到弗成见的状态,也便是在onStop()措施中将线程竣事,在onStart()措施中开启线程。否则,Timer没有竣事,或者反复开启,会引起较大年夜的内存耗损,光阴一长就法度榜样就会崩掉落。 还有,便是在跳转到其他Activity的历程中会呈现画面的卡顿

下面黄金城棋牌游戏网址看一下效果图和详细代码:

工程布局如下图所示:

main.xml:

P>

P>P>STRONG>然后是详细的结构文件及代码实现:STRONG>P>P>main.xml:P> xml version="1.0" encoding="utf-8"?>

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"

android:layout_height="fill_parent"android:background="#FFFFFF"

android:orientation="vertical" >

RelativeLayoutandroid:layout_width="fill_parent"

android:layout_height="40dip"android:background="@drawable/title_bk" >

ImageButton

android:id="@+id/btn_back"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:background="@drawable/btn_back_selector"

android:src="@drawable/btn_back" />

Viewandroid:id="@+id/line0"

android:layout_width="1px"android:layout_height="fill_parent"

android:layout_toRightOf="@id/btn_back"android:background="#aa11264f" />

View

android:layout_width="1px"android:layout_height="fill_parent"

android:layout_toRightOf="@id/line0"android:background="#009ad6" />

TextView

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:layout_centerInParent="true"android:text="优酷客户端"

android:textColor="#FFFFFF"android:textSize="20sp" />

RelativeLayout>

FrameLayoutandroid:layout_width="fill_parent"

android:layout_height="140dip" >

android.support.v4.view.ViewPagerandroid:id="@+id/vp"

android:layout_width="fill_parent"android:layout_height="fill_parent" />

LinearLayout

android:layout_width="fill_parent"android:layout_height="35dip"

android:layout_gravity="bottom"android:background="#33000000"

android:gravity="center"android:orientation="vertical" >

TextView

android:id="@+id/tv_title"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="中国家庭院校园区域名字表现"

android:textColor="#ffffff" />

LinearLayoutandroid:layout_width="wrap_content"

android:layout_height="wrap_content"android:layout_marginTop="3dip"

android:gravity="center" >

Viewandroid:id="@+id/v_dot0"

style="@style/dot_style"android:background="@drawable/dot_focused" />

View

android:id="@+id/v_dot1"style="@style/dot_style" />

View

android:id="@+id/v_dot2"style="@style/dot_style" />

View

android:id="@+id/v_dot3"style="@style/dot_style" />

View

android:id="@+id/v_dot4"style="@style/dot_style" />

LinearLayout>LinearLayout>

FrameLayout>

LinearLayout>

MyViewPagerActivity:

package com.tony.viewpager;

import java.util.ArrayList;

import java.util.List;import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;

import android.app.Activity;import android.os.Bundle;

import android.os.Handler;import android.os.Parcelable;

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.widget.ImageView;import android.widget.ImageView.ScaleType;

import android.widget.TextView;

/奸淫 仿优酷Android客户端图片阁下滑动

**/

public class MyViewPagerActivity extends Activity {private ViewPager viewPager; // android-support-v4中的滑动组件

private List imageViews; // 滑动的图片聚拢

private String[] titles; // 图片标题private int[] imageResId; // 图片ID

private List dots; // 图片标题正文的那些点

private TextView tv_title;private int currentItem = 0; // 当前图片的索引号

// An ExecutorService that can schedule commands to run after a given delay,

// or to execute periodically.private ScheduledExecutorService scheduledExecutorService;

// 切换当前显示的图片

private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {

viewPager.setCurrentItem(currentItem);// 切换当前显示的图片};

};

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

imageResId = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };

titles = new String[imageResId.length];titles[0] = "巩俐不低俗,我就不能低俗";

titles[1] = "扑树又回来啦!再唱经典老歌引万人大年夜合唱";titles[2] = "揭秘北京片子若何进级";

titles[3] = "乐视网TV版大年夜派送";titles[4] = "热血屌丝的反杀";

imageViews = new ArrayList();

// 初始化图片资本

for (int i = 0; iImageView imageView = new ImageView(this);

imageView.setImageResource(imageResId[i]);imageView.setScaleType(ScaleType.CENTER_CROP);

imageViews.add(imageView);}

dots = new ArrayList();dots.add(findViewById(R.id.v_dot0));

dots.add(findVie黄金城棋牌游戏网址wById(R.id.v_dot1));dots.add(findViewById(R.id.v_dot2));

dots.add(findViewById(R.id.v_dot3));dots.add(findViewById(R.id.v_dot4));

tv_title = (TextView) findViewById(R.id.tv_title);

tv_title.setText(titles[0]);//

viewPager = (ViewPager) findViewById(R.id.vp);viewPager.setAdapter(new MyAdapter());// 设置添补ViewPager页面的适配器

// 设置一个监听器,当ViewPager中的页面改变时调用viewPager.setOnPageChangeListener(new MyPageChangeListener());

}

@Override

protected void onStart() {scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

// 当Activity显示出来后,每两秒钟切换一次图片显示scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 2, TimeUnit.SECONDS);

super.onStart();}

@Override

protected void onStop() {// 当Activity弗成见的时刻竣事切换

scheduledExecutorService.shutdown();super.onStop();

}

/奸淫 换行切换义务

** @author Administrator

**/

private class ScrollTask implements Runnable {

public void run() {synchronized (viewPager) {

System.out.println("currentItem: " + currentItem);currentItem = (currentItem + 1) % imageViews.size();

handler.obtainMessage().sendToTarget(); // 经由过程Handler切换图片}

}

}

/奸淫 当ViewPager中页面的状态发生改变时调用

** @author Administrator

**/

private class MyPageChangeListener implements OnPageChangeListener {private int oldPosition = 0;

/**

* This method will be invoked when a new page becomes selected.* position: Position index of the new selected page.

*/public void onPageSelected(int position) {

currentItem = position;tv_title.setText(titles[position]);

dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);dots.get(position).setBackgroundResource(R.drawable.dot_focused);

oldPosition = position;}

public void onPageScrollStateChanged(int arg0) {

}

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

}

/奸淫 添补ViewPager页面的适配器

** @author Administrator

**/

private class MyAdapter extends PagerAdapter {

@Overridepublic int getCount() {

return imageResId.length;}

@Override

public Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(imageViews.get(arg1));

return imageViews.get(arg1);}

@Override

public void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView((View) arg2);

}

@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {

return arg0 == arg1;}

@Override

public void restoreState(Parcelable arg0, ClassLoader arg1) {

}

@Overridepublic Parcelable saveState() {

return null;}

@Override

public void startUpdate(View arg0) {

}

@Overridepublic void finishUpdate(View arg0) {

}

}}

Drawable目录下

btn_back_selector.xml:

xml version="1.0" encoding=黄金城棋牌游戏网址"utf-8"?>

selector xmlns:android="http://schemas.android.com/apk/res/android">

item android:drawable="@drawable/btn_top_pressed" android:state_focused="true">item>item android:drawable="@drawable/btn_top_pressed" android:state_pressed="true">item>

item android:drawable="@drawable/btn_top_pressed" android:state_selected="true">item>item android:drawable="@drawable/title_bk">item>

selector>

btn_top_pressed.xml:

xml version="1.0" encoding="utf-8"?>

shape xmlns:android="http://schemas.android.com/apk/res/android"黄金城棋牌游戏网址android:shape="rectangle" >

gradient

android:angle="270"android:endColor="#009ad6"

android:startColor="#11264f" />

shape>

dot_focused.xml:

xml version="1.0" encoding="utf-8"?>

shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval" >

solid android:color="#aaFFFFFF" />

corners android:radius="5dip" />

shape>

dot_normal.xml:

xml version="1.0" encoding="utf-8"?>

shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval" >

solid android:color="#33000000" />

corners android:radius="5dip" />

shape>

title_bk.xml:

xml version="1.0" encoding="utf-8"?>

shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

gradient

android:angle="270" android:endColor="#11264f"

android:startColor="#009ad6" />

shape>

转自:http://blog.csdn.net/t12x3456/article/details/8160128

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: