图片的自动轮换

本文介绍了一个基于Android的应用案例,实现了一种带有指示器的轮播图效果,并通过ViewPager实现了图片的自动切换。此外,还展示了如何使用TextView实现文字的自动滚动,适用于新闻或者公告展示场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下为效果图:


下面直接上代码:

Java部分代码:

package com.example.project_0617;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zking.db.DbHelper;
import com.example.zking.db.DbManager;
import com.example.zking.entity.xuebaTK;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/6/21.
 */

public class Message_Activity extends AppCompatActivity {

    private int[] id;
    private ArrayList<View> arrayList;
    private ViewPager vp;
    private int oldPosition;
    private int currentItem;
    private ScheduledExecutorService scheduledExecutorService;
    private ArrayList<RadioButton> buttons;
    private RadioGroup rg;
    private int[] radioB;
    private TextView tv1;
    private List<xuebaTK> list=new ArrayList<>();

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

        id = new int[]{R.drawable.x1,R.drawable.x2,R.drawable.x3,R.drawable.x4,R.drawable.x5,R.drawable.x6};
        arrayList = new ArrayList<>();
        vp = (ViewPager) findViewById(R.id.vp_mes_pager);
        rg = (RadioGroup) findViewById(R.id.rg_mes_group);
        oldPosition = 0;
        radioB = new int[]{R.id.rb_mes_button1,R.id.rb_mes_button2,R.id.rb_mes_button3,R.id.rb_mes_button4,R.id.rb_mes_button5,R.id.rb_mes_button6};
        buttons = new ArrayList<>();

        
        for (int i = 0; i < id.length; i++) {
            ImageView iv=new ImageView(this);
            iv.setImageResource(id[i]);
            arrayList.add(iv);
        }
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button1));
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button2));
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button3));
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button4));
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button5));
        buttons.add((RadioButton) findViewById(R.id.rb_mes_button6));

        vp.setAdapter(new AdapterDIY());
        rg.check(radioB[0]);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                buttons.get(position).isChecked();
                oldPosition=position;
                currentItem=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(state==2){
                    rg.check(radioB[vp.getCurrentItem()]);
                }
            }
        });
    }

    class AdapterDIY extends PagerAdapter{

        @Override
        public int getCount() {
            return arrayList.size();
        }

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v=arrayList.get(position);
            container.addView(v);
            return v;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        scheduledExecutorService= Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(),2,2, TimeUnit.SECONDS);
    }

    class ViewPagerTask implements Runnable{

       @Override
       public void run() {
           currentItem=(currentItem+1)%id.length;
            handler.obtainMessage().sendToTarget();
       }
   }
   Handler handler=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           vp.setCurrentItem(currentItem);
       }
   };

}

Xml部分代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/b5"
    >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        >

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="260dp"
            android:id="@+id/vp_mes_pager"
            android:alpha="0.9"
            >
        </android.support.v4.view.ViewPager>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center"
                android:id="@+id/rg_mes_group"
                >
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button1"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button2"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button3"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button4"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button5"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_mes_button6"
                    />
            </RadioGroup>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:text="@string/mes_tv1"
                android:gravity="center"
                android:textSize="18dp"
                android:background="#afb3eaff"
                android:layout_marginBottom="5dp"
                android:id="@+id/tv_mes_view1"
                android:singleLine="true"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:text="@string/mes_tv2"
                android:gravity="center"
                android:textSize="18dp"
                android:background="#afb3eaff"
                android:layout_marginBottom="5dp"
                android:id="@+id/tv_mes_view2"
                android:singleLine="true"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:text="@string/mes_tv3"
                android:gravity="center"
                android:textSize="18dp"
                android:background="#afb3eaff"
                android:layout_marginBottom="5dp"
                android:id="@+id/tv_mes_view3"
                android:singleLine="true"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:text="@string/mes_tv4"
                android:gravity="center"
                android:textSize="18dp"
                android:background="#afb3eaff"
                android:layout_marginBottom="5dp"
                android:id="@+id/tv_mes_view4"
                android:singleLine="true"
                android:scrollHorizontally="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                />

           
        </LinearLayout>


   </LinearLayout>


</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值