我在思考如何使用ImageSwitchers和Gallery进行自动幻灯片放映时遇到了麻烦。我唯一理解的是当您从图库中选择图片时转换图片之间的动画,例如它们从左向右移动或移动的方式。
修改
我的目的是在创建图库并选择每张图片然后使用imageswitcher在底部布局上显示所选图片时,尝试以相同的方式制作幻灯片。就在这个时候,实际上是点击,我想让图片自动移动(下一个在最后一个之后),就像在Powerpoint上和显然有一个计时器一样。
这是全班: package project.CaseStudy;
public class Slideshow extends Activity implements ViewFactory {
//---the images to display---
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
TextView legend;
TextView imageID;
CheckBox repeat;
CheckBox image;
CheckBox text;
public int picture;
public int pictureCurrentlySelected;
private ImageSwitcher imageSwitcher;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
legend = (TextView) findViewById(R.id.legend);
imageID = (TextView) findViewById(R.id.imagenum);
repeat = (CheckBox) findViewById(R.id.repeat);
image = (CheckBox) findViewById(R.id.number);
text = (CheckBox) findViewById(R.id.text);
//text.setEnabled(true);
//image.setEnabled(true);
//repeat.setEnabled(true);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
/*imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
*/
pictureCurrentlySelected = -1;
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
imageSwitcher.setImageResource(imageIDs[position]);
picture = position;
pictureCurrentlySelected = picture;
check();
}
});
}
public View makeView()
{
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return imageView;
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount()
{
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position)
{
return position;
}
//---returns the ID of an item---
public long getItemId(int position)
{
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
public void onCheckBoxClickListener(View v)
{
boolean checked = ((CheckBox) v).isChecked();
switch(v.getId()){
case R.id.repeat:
/*if(checked)
{
return;
}*/
break;
case R.id.text:
if(!checked)
legend.setText("");
else
{
if(pictureCurrentlySelected == -1)
{
legend.setText("");
}
else
check();
}
break;
case R.id.number:
if(!checked)
imageID.setText("");
else
{
if(pictureCurrentlySelected == -1)
{
legend.setText("");
}
else
check();
}
break;
}
}
private void check()
{
if(text.isChecked())
{
if(picture == 0)
legend.setText("This is San Fransisco's Bridge. A beautiful Sight!");
else if(picture == 1)
legend.setText("This is the train used to pick up the bitches around town.");
else if(picture == 2)
legend.setText("The outstanding lake of San Fransisco");
else if(picture == 3)
legend.setText("A beautiful view of the city!");
else if(picture == 4)
legend.setText("Beauty at its limit.");
else if(picture == 5)
legend.setText("Town at sunset.");
else
legend.setText("Bridge during within a beautiful sight.");
}
/*else
legend.setText("");*/
if(image.isChecked())
{
if(picture == 0)
imageID.setText("Image number 1");
else if(picture == 1)
imageID.setText("Image number 2");
else if(picture == 2)
imageID.setText("Image number 3");
else if(picture == 3)
imageID.setText("Image number 4");
else if(picture == 4)
imageID.setText("Image number 5");
else if(picture == 5)
imageID.setText("Image number 6");
else
imageID.setText("Image number 7");
}
//else
//legend.setText("");
if(repeat.isChecked())
{
}
//else;
}
}
忽略最后两种方法,如果它太复杂而无法理解我只是草绘我的程序并最终将在以后修复冗余。
这是XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Images of San Francisco" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckBoxClickListener"
android:text="Repeat" />
<CheckBox
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckBoxClickListener"
android:text="Image ID" />
<CheckBox
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckBoxClickListener"
android:text="Text" />
</LinearLayout>
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50" >
<ImageSwitcher
android:id="@+id/switcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageSwitcher>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image number: " />
<TextView
android:id="@+id/imagenum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/legend"
android:layout_width="match_parent"
android:layout_height="57dp"
android:text="" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
我不确定你想要实现的目标,但我所知道的创建小型幻灯片的最佳(阅读:最简单)方式就是简单地放置你的imageViews和/或textViews进入框架布局。然后在您的代码中,您将使用switch语句来设置每个图像的可见性。
例如。案例0:
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.INVISIBLE);
等...