作为标题,我需要对我正在研究的大学项目提供一些帮助。 我必须在android中创建一个Activity,给定一组图像运行带有计时器的幻灯片。这项活动分为三个部分:
此外,如果在中间进行滑动,则必须根据滑动的方向转到上一个或下一个。 我已经制作了布局xml和acitivity。有人可以帮助我,甚至可能是一丝一毫的?非常感谢!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<Button
android:id="@+id/button_start_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/button_stop"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text_view_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/image_view"
android:layout_marginTop="76dp"
android:maxLines="3"
android:contentDescription="@string/text_view_desc"
android:text="@string/default_notes"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/image_view_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image_view_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/image_view"
android:layout_alignParentBottom="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image_view_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Slideshow extends Activity{
private Button startstop;
public ImageView ivdiplayed, ivprevious, ivcurrent, ivnext;
boolean isPlaying = true; //true=play | false=stop
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
//imageview
ivdiplayed = (ImageView) findViewById(R.id.image_view);
ivprevious = (ImageView) findViewById(R.id.image_view_previous);
ivcurrent = (ImageView) findViewById(R.id.image_view_current);
ivnext = (ImageView) findViewById(R.id.image_view_next);
//play/stop button
startstop = (Button) findViewById(R.id.button_start_stop);
startstop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isPlaying){
startstop.setText(getString(R.string.button_stop));
isPlaying=false;
}
else{
startstop.setText(getString(R.string.button_play));
isPlaying=true;
}
}
});
}
}
答案 0 :(得分:0)
为什么不使用ViewPager?这样可以更轻松地滑动图像 您可以使用ViewPager替换中间(主)图像。以下是步骤:
首先,我们编辑页面布局[注意ViewPager]
的 mainLayout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<Button
android:id="@+id/button_start_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/button_stop"
android:textColor="#FFFFFF" />
<android.support.v4.view.ViewPager
android:id="@+id/pagr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/text_view_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/image_view"
android:layout_marginTop="76dp"
android:maxLines="3"
android:contentDescription="@string/text_view_desc"
android:text="@string/default_notes"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/image_view_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image_view_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/image_view"
android:layout_alignParentBottom="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image_view_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:contentDescription="@string/img_view_desc"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
然后我们创建将在ViewPager中显示的Image项目视图
的 imageitem.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="my mage" />
之后我们需要创建适配器来处理ViewPager中的这些图像
ImageAdapter.Java类
public class ImageAdapter extends PagerAdapter {
// list of images resources ids
private ArrayList<Integer> items;
private Context context;
public ImageAdapter(Context context, ArrayList<Integer> items) {
super();
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object instantiateItem(View collection, int position) {
View v = null;
LayoutInflater vi = null;
if (v == null) {
vi = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.imageitem, null);
}
}
// sets the image resource to our ImageView item
ImageView imageView = (ImageView) v.findViewById(R.id.imageItem);
imageView.setImageResource(items.get(position));
// add the image to ViewPager
((ViewPager) collection).addView(v);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public Parcelable saveState() {
return null;
}
}
最后我们的MainActivity类
Slideshow.Java类
import android.app.Activity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Slideshow extends Activity{
private ViewPager viewPager;
private ImageAdapter adapter;
private Button startstop;
private Thread timerThread;
public ImageView ivdiplayed, ivprevious, ivcurrent, ivnext;
boolean isPlaying = true; //true=play | false=stop
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
// Setup Images List
ArrayList<Integer> imageResources = new ArrayList<Integer>();
imageResources.add(R.drawable.image1);
imageResources.add(R.drawable.image2);
imageResources.add(R.drawable.image3); // etc.. add all images you got
// Initialize our timer thread
timerThread = new Thread(this);
//Init our ViewPager
viewPager = (ViewPager) findViewById(R.id.pagr);
//Init our Adapter and pass it the list of images
adapter = new ImagesAdapter(this, imageResources);
// Set the adapter to the view
viewPager.setAdapter(adapter);
ivprevious = (ImageView) findViewById(R.id.image_view_previous);
ivcurrent = (ImageView) findViewById(R.id.image_view_current);
ivnext = (ImageView) findViewById(R.id.image_view_next);
// To detect when image changes
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int position) {
if(position>0)// No images before
ivprevious.setImageResource(imageResources.get(position-1));
ivcurrent.setImageResource(imageResources.get(position-1));
if(position<(imageResources.size()-1))// No images after
ivnext.setImageResource(imageResources.get(position+1));
}
});
//play/stop button
startstop = (Button) findViewById(R.id.button_start_stop);
startstop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isPlaying){
startstop.setText(getString(R.string.button_stop));
isPlaying=false;
}
else{
/*Start our timer.. You can use AlarmManger, but I think this will do the trick*/
timerThread.start();
startstop.setText(getString(R.string.button_play));
isPlaying=true;
}
}
});
}
// Our Timer thread function
public void run() {
// The position of the current image shown in the ViewPager
int position;
while (isPlaying) { //condition to exit thread loop
//Get which image is selected in the ViewPager
position = viewPager.getCurrentItem();
try {
// Time between each slide in MilliSeconds
Thread.sleep(1000);
// Move to next image if available OR go to first one
if(position<(imageResources.size()-1)){
viewPager.setCurrentItem(position+1);
}else{
viewPager.setCurrentItem(0);
}
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
}
}
}
希望这对你有所帮助,有很多更好的方法,但我认为这是一种简单易行的方法
另外..我没有测试它,所以你可能需要一些小的修复...抱歉