Android实现图片文字轮播特效
作者:xu佳佳 时间:2021-07-25 18:44:11
本文实例讲解了Android实现图片文字轮播特效的详细代码,分享给大家供大家参考,具体内容如下
图片轮播是类似知乎日报上的一个轮播效果,如下图。
好了直接进入正题,首先是出示一下效果:
MainActivity:
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.os.Bundle;
import android.app.Activity;
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.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
//viewpager
private ViewPager view_pager;
private LinearLayout ll_dotGroup;
private TextView newsTitle;
private int imgResIds[] = new int[]{R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d, R.drawable.b};
//存储5张图片
private String textview[]=new String[]{"12412515125","fawfafawf"
,"13f1f12f211","1251f1f12","1t1f12f121"};
//存储5个目录
private int curIndex = 0;
//用来记录当前滚动的位置
PicsAdapter picsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setViewPager();
}
private void setViewPager() {
newsTitle=(TextView)findViewById(R.id.NewsTitle);
view_pager = (ViewPager) findViewById(R.id.view_pager);
ll_dotGroup = (LinearLayout) findViewById(R.id.dotgroup);
picsAdapter = new PicsAdapter(); // 创建适配器
picsAdapter.setData(imgResIds);
view_pager.setAdapter(picsAdapter); // 设置适配器
view_pager.setOnPageChangeListener(new MyPageChangeListener()); //设置页面切换 *
initPoints(imgResIds.length); // 初始化图片小圆点
startAutoScroll(); // 开启自动播放
}
// 初始化图片轮播的小圆点和目录
private void initPoints(int count) {
for (int i = 0; i < count; i++) {
ImageView iv = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
20, 20);
params.setMargins(0, 0, 20, 0);
iv.setLayoutParams(params);
iv.setImageResource(R.drawable.dot1);
ll_dotGroup.addView(iv);
}
((ImageView) ll_dotGroup.getChildAt(curIndex))
.setImageResource(R.drawable.dot2);
newsTitle.setText(textview[curIndex]);
}
// 自动播放
private void startAutoScroll() {
ScheduledExecutorService scheduledExecutorService = Executors
.newSingleThreadScheduledExecutor();
// 每隔4秒钟切换一张图片
scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(), 5,
4, TimeUnit.SECONDS);
}
// 切换图片任务
private class ViewPagerTask implements Runnable {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int count = picsAdapter.getCount();
view_pager.setCurrentItem((curIndex + 1) % count);
}
});
}
}
// 定义ViewPager控件页面切换 *
class MyPageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
ImageView imageView1 = (ImageView) ll_dotGroup.getChildAt(position);
ImageView imageView2 = (ImageView) ll_dotGroup.getChildAt(curIndex);
if (imageView1 != null) {
imageView1.setImageResource(R.drawable.dot2);
}
if (imageView2 != null) {
imageView2.setImageResource(R.drawable.dot1);
}
curIndex = position;
newsTitle.setText(textview[curIndex]);
}
boolean b = false;
@Override
public void onPageScrollStateChanged(int state) {
//这段代码可不加,主要功能是实现切换到末尾后返回到第一张
switch (state) {
case 1:// 手势滑动
b = false;
break;
case 2:// 界面切换中
b = true;
break;
case 0:// 滑动结束,即切换完毕或者加载完毕
// 当前为最后一张,此时从右向左滑,则切换到第一张
if (view_pager.getCurrentItem() == view_pager.getAdapter()
.getCount() - 1 && !b) {
view_pager.setCurrentItem(0);
}
// 当前为第一张,此时从左向右滑,则切换到最后一张
else if (view_pager.getCurrentItem() == 0 && !b) {
view_pager.setCurrentItem(view_pager.getAdapter()
.getCount() - 1);
}
break;
default:
break;
}
}
}
// 定义ViewPager控件适配器
class PicsAdapter extends PagerAdapter {
private List<ImageView> views = new ArrayList<ImageView>();
@Override
public int getCount() {
if (views == null) {
return 0;
}
return views.size();
}
public void setData(int[] imgResIds) {
for (int i = 0; i < imgResIds.length; i++) {
ImageView iv = new ImageView(MainActivity.this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
//设置ImageView的属性
iv.setImageResource(imgResIds[i]);
views.add(iv);
}
}
public Object getItem(int position) {
if (position < getCount())
return views.get(position);
return null;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(View container, int position, Object object) {
if (position < views.size())
((ViewPager) container).removeView(views.get(position));
}
@Override
public int getItemPosition(Object object) {
return views.indexOf(object);
}
@Override
public Object instantiateItem(View container, int position) {
if (position < views.size()) {
final ImageView imageView = views.get(position);
((ViewPager) container).addView(imageView);
return views.get(position);
}
return null;
}
}
}
activity_main:
<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"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="5dp"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/focus_bg"
>
<TextView
android:id="@+id/NewsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我校交换生演讲比赛夺冠 美国华盛本大学万里发来贺电"
android:paddingTop="10dp"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="100dp"
android:textSize="15sp"
android:textColor="#fff"/>
<LinearLayout
android:id="@+id/dotgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="23dp"
android:paddingRight="10dp"
android:gravity="center"
android:layout_marginBottom="15dp"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
标签:Android,轮播
0
投稿
猜你喜欢
基于IDEA中格式化代码的快捷键分享
2022-08-15 14:58:03
Android viewpager自动轮播和小圆点联动效果
2023-08-06 21:42:41
Maven依赖管理的用法介绍
2021-07-13 18:07:35
Android调用OpenCV2.4.10实现二维码区域定位
2023-06-05 10:17:32
Spring主配置文件(applicationContext.xml) 导入约束详解
2021-09-29 06:21:01
Java BigDecimal除法精度和格式化输出方式
2023-01-06 01:03:42
解决idea中yml文件不识别的问题
2021-12-07 02:09:28
java使用链表实现约瑟夫环
2022-03-21 18:59:35
Android通过自定义ImageView控件实现图片的缩放和拖动的实现代码
2022-03-02 22:52:08
Java 8新增的方法参数反射实例分析
2021-11-20 05:55:30
Java读取TXT文件内容的方法
2023-11-23 22:33:41
java poi导出图片到excel示例代码
2023-10-30 00:13:17
c#数据绑定之将datatabel的data添加listView
2022-01-02 03:02:33
Java9中对集合类扩展的of方法解析
2022-06-10 09:49:43
Oracle + Mybatis实现批量插入、更新和删除示例代码
2022-01-18 16:05:58
GraalVM native-image编译后quarkus的超音速启动
2022-08-09 01:17:27
Android自定义ImageView实现在图片上添加图层效果
2023-01-12 01:03:32
Android判断手机是否联网及自动跳转功能(收藏版)
2023-10-06 18:42:33
Android使用shape使组件呈现出特殊效果的方法
2022-06-28 03:42:27
Java中CyclicBarrier和CountDownLatch的用法与区别
2021-09-09 08:20:01