android实现图片+文字轮播(带文字)
来源:互联网 发布:北京十大网络工程公司 编辑:程序博客网 时间:2024/04/28 16:19
activity_main.xml
<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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="160dp" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:background="#66000000" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textColor="#fff" android:textSize="20dp" android:text="天王盖地虎,天王盖地虎,天王盖地虎," /> <LinearLayout android:id="@+id/ll_point" android:gravity="center_horizontal" android:orientation="horizontal" android:layout_marginTop="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout></RelativeLayout>
MainActivity.java
package com.example.test;import java.util.ArrayList;import java.util.List;import android.app.Activity;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.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity { private ViewPager viewpager; private List<ImageView> ivList; private String[] descContent; private TextView tv; private LinearLayout ll_point; private int previousPointPosition = 0; private boolean isRunning = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initData(); // 自动轮询播放 new Thread(new Runnable() { @Override public void run() { while (isRunning) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { viewpager.setCurrentItem(viewpager.getCurrentItem() + 1); } }); } } }).start(); } @Override protected void onDestroy() {//按返回键,就被摧毁,就停止循环 // TODO Auto-generated method stub super.onDestroy(); isRunning = false; System.out.println("-----onDestroy"); } private void initViews() { viewpager = (ViewPager) this.findViewById(R.id.viewpager); tv = (TextView) this.findViewById(R.id.textview); ll_point = (LinearLayout) this.findViewById(R.id.ll_point); } private void initData() { ivList = new ArrayList<ImageView>(); descContent = new String[] { "窗前明月光", "地上鞋两双", "床上狗男女", "其中必有你", "真的有我" }; int[] imgsId = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; for (int i = 0; i < imgsId.length; i++) { ImageView iv = new ImageView(this); iv.setBackgroundResource(imgsId[i]); ivList.add(iv); View pointView = new View(this); pointView.setBackgroundResource(R.drawable.selector_bg_point); pointView.setEnabled(false); // 设置圆点的高度和宽度为5 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 15, 15); if (i != 0) { layoutParams.leftMargin = 10; } ll_point.addView(pointView, layoutParams); } tv.setText(descContent[0]);// 设置第一个TextView的值 ll_point.getChildAt(0).setEnabled(true);// 设置第一个圆点可用 viewpager.setAdapter(new MyAdapter()); viewpager.setOnPageChangeListener(new MyPageChangeListener()); // 从0滑到Integer.MAX_VALUE,需要63年 int currentPosition = (Integer.MAX_VALUE - 1) / 2 - 3; viewpager.setCurrentItem(currentPosition);// Integer.MAX_VALUE的值:2147483647 } class MyAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE;// Integer.MAX_VALUE的值:2147483647 } @Override public boolean isViewFromObject(View view, Object obj) { return view == obj; } @Override public Object instantiateItem(ViewGroup container, int position) { System.out.println("---position:" + position); ImageView iv = ivList.get(position % 5); ((ViewPager) container).addView(iv); return iv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((View) object); } } class MyPageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { int newPosition = position % 5; tv.setText(descContent[newPosition]); ll_point.getChildAt(previousPointPosition).setEnabled(false); ll_point.getChildAt(newPosition).setEnabled(true); previousPointPosition = newPosition; } }}
实验截图:
源码下载地址:
http://download.csdn.net/detail/u013209983/9739777
0 0
- android实现图片+文字轮播(带文字)
- android带文字、可点击轮播的实现
- android 图片文字轮播效果(图片和文字自动滚动)
- android 图片文字轮播效果(图片和文字自动滚动)
- Android 文字轮播
- [html5]轮播文字/图片
- 文字轮播与图片轮播
- android imagebutton 带文字图片的实现方法
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Androidの实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- [Codeforces 232B] Table (计数 + 背包DP)
- 为什么说java是面向对象呢?
- nc与udp
- codeforces 750E New Year and Old Subsequence(线段树+矩阵建模)
- 【转载】Java:按值传递还是按引用传递详细解说
- android实现图片+文字轮播(带文字)
- 使用jsonp方式实现ajax跨域请求
- 摄像头驱动之实现数据传输2_简单函数_学习笔记
- Nginx并发数、每秒连接数、下载速度限制,防攻击杀手锏
- mnode第三篇 在mysql之上搭建redis缓存
- PyQt5的学习之路(五)
- Python日常:json.loads&json.dumps的使用
- docker学习
- org.apache.commons.net.util.Base64的encodeBase64String方法的坑