Android 自动滑动图像。
我已经创建了一个演示项目来幻灯片图像。打开应用程序后,我需要图像自动滑动。我在下面发布我的代码 活动\页面\视图(主布局)Android 自动滑动图像。,android,Android,我已经创建了一个演示项目来幻灯片图像。打开应用程序后,我需要图像自动滑动。我在下面发布我的代码 活动\页面\视图(主布局) PageViewActivity(主活动) 导入android.support.v4.app.Fragment; 导入android.support.v4.app.FragmentActivity; 导入android.support.v4.app.FragmentManager; 导入android.support.v4.app.FragmentPagerAdapte
PageViewActivity(主活动)
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.PagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.os.Bundle;
导入java.util.ArrayList;
导入java.util.List;
公共类PageViewActivity扩展了FragmentActivity{
MyPageAdapter-pageAdapter;
查看寻呼机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u page\u视图);
List fragments=getFragments();
pageAdapter=新的MyPageAdapter(getSupportFragmentManager(),片段);
pager=(ViewPager)findViewById(R.id.ViewPager);
寻呼机设置适配器(pageAdapter);
}
私有列表getFragments(){
List fList=new ArrayList();
add(MyFragment.newInstance(1));
add(MyFragment.newInstance(2));
add(MyFragment.newInstance(3));
add(MyFragment.newInstance(4));
add(MyFragment.newInstance(5));
回传;
}
}
我的碎片
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class MyFragment extends Fragment {
public static final String IMAGE_URL = "IMAGE_URL";
TextView messageTextView;
ImageView imageView;
public ArrayList<String> UrlsImage = new ArrayList<String>();
public ArrayList<String> TagLine = new ArrayList<String>();
public static MyFragment newInstance(Integer index)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putInt(IMAGE_URL,index);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int index = getArguments().getInt(IMAGE_URL);
UrlsImage.add("http://itradar.ir/wp-content/uploads/2016/08/root-android-5.jpg");
UrlsImage.add("http://www.pcwebim.com/wp-content/uploads/2015/12/Android.jpg");
UrlsImage.add("http://www.androidrootguide.com/wp-content/uploads/2014/10/Stock-Android-Wallpapers-Download.jpg");
UrlsImage.add("https://ardroid.com/wp-content/uploads/2011/01/wpaper1294246633612.jpg");
UrlsImage.add("http://www.androidcentral.com/sites/androidcentral.com/files/styles/w550h500/public/wallpapers/black-lloyd-7dk.jpg?itok=bGsIaB2R");
TagLine.add("Android Pirate");
TagLine.add("Android Alien");
TagLine.add("Android Perfect");
TagLine.add("Android Halloween");
TagLine.add("Android Black");
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
messageTextView = (TextView)v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageview);
Glide.with(this)
.load(UrlsImage.get(index-1))
.into(imageView);
messageTextView.setText(TagLine.get(index-1));
return v;
}
}
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.bumptech.glide.glide;
导入java.util.ArrayList;
公共类MyFragment扩展了Fragment{
公共静态最终字符串IMAGE\u URL=“IMAGE\u URL”;
文本视图消息文本视图;
图像视图图像视图;
public ArrayList UrlsImage=new ArrayList();
public ArrayList TagLine=new ArrayList();
公共静态MyFragment newInstance(整数索引)
{
MyFragment f=新的MyFragment();
Bundle bdl=新Bundle(1);
bdl.putInt(图像、URL、索引);
f、 设置参数(bdl);
返回f;
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
int index=getArguments().getInt(图像URL);
UrlsImage.add(“http://itradar.ir/wp-content/uploads/2016/08/root-android-5.jpg");
UrlsImage.add(“http://www.pcwebim.com/wp-content/uploads/2015/12/Android.jpg");
UrlsImage.add(“http://www.androidrootguide.com/wp-content/uploads/2014/10/Stock-Android-Wallpapers-Download.jpg");
UrlsImage.add(“https://ardroid.com/wp-content/uploads/2011/01/wpaper1294246633612.jpg");
UrlsImage.add(“http://www.androidcentral.com/sites/androidcentral.com/files/styles/w550h500/public/wallpapers/black-lloyd-7dk.jpg?itok=bGsIaB2R");
添加(“安卓海盗”);
标语。添加(“安卓外星人”);
添加(“安卓完美”);
添加(“安卓万圣节”);
添加(“安卓黑色”);
视图v=充气机。充气(R.layout.myfragment\u布局,容器,假);
messageTextView=(TextView)v.findViewById(R.id.TextView);
imageView=(imageView)v.findViewById(R.id.imageView);
用(这个)滑翔
.load(UrlsImage.get(索引-1))
.进入(图像视图);
messageTextView.setText(TagLine.get(index-1));
返回v;
}
}
MyPageAdapter(自定义适配器)
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入java.util.List;
公共类MyPageAdapter扩展了FragmentPagerAdapter{
私有列表片段;
公共MyPageAdapter(FragmentManager fm,列出片段){
超级(fm);
这个。碎片=碎片;
}
@凌驾
公共片段getItem(int位置){
返回此.fragments.get(位置);
}
@凌驾
public int getCount(){
返回此.fragments.size();
}
}
myu布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:id="@+id/imageview"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:textColor="@color/colorAccent"/>
</RelativeLayout>
我应该在哪里更改代码。
希望得到帮助。提前谢谢。您可以在
onCreate()中使用类似的处理程序和计时器来执行此操作。
:
我想你可能会对一份工作感兴趣。这基本上是一个有动画师的框架布局 从文档中: 将在两个或多个视图之间设置动画的简单ViewAnimator 已添加到其中。一次只显示一个子对象。如果 请求时,可以定期在每个孩子之间自动切换 间隔时间 只需将此方法添加到您的活动/片段中 在
onCreate
中,只需调用一次即可
new Handler().postDelayed(update,1000);
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments=fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:id="@+id/imageview"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:textColor="@color/colorAccent"/>
</RelativeLayout>
final Handler handler = new Handler();
final Runnable update = new Runnable() {
public void run() {
if (position == TOTAL_FRAMENT- 1) {
position = 0;
} else {
position++;
}
pager.setCurrentItem(position, true);
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.post(update);
}
}, 100, 1000);
private Runnable update = new Runnable() {
public void run() {
//get the position from viewPager.addOnPageChangeListener
if (position == mAdapter.getCount()- 1) {
position = 0;
} else {
position++;
}
pager.setCurrentItem(position, true);
handler.post(update);
}
};
new Handler().postDelayed(update,1000);