标签:android
ImageSwitcher继承了ViewSwitcher,所以在切换时候会有动画,可以把它理解成一个动画版本的ImageView。
他的showNext(),和showPrevious()方法分别显示下一个和上一个。
为ImageSwitcher提供一个ViewFactory,该ViewFactory的makeView()生成的View必须是ImageView。
ImageSwicher可以用setimageResource()和色图ImageDrawable()以及setImageUri()来设置图片资源。
package peng.liu.test;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private int[] imageIds = new int[]{
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher
};
ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0;i<imageIds.length;i++){
Map<String,Object> item = new HashMap<String, Object>();
item.put("image",imageIds[i]);
listItems.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,listItems,R.layout.cell,new String[]{"image"},new int[]{R.id.image02});
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView image = new ImageView(MainActivity.this);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
return image;
}
});
GridView grid = (GridView) findViewById(R.id.grid);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
imageSwitcher.setImageResource(imageIds[i]);
}
});
grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
imageSwitcher.setImageResource(imageIds[i]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
};
}
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1pt"
android:verticalSpacing="2pt"
android:numColumns="4"
android:gravity="center"
android:id="@+id/grid"
/>
<ImageSwitcher
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageSwitcher"
android:layout_gravity="center_horizontal"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
/>
</LinearLayout>
TextSwitcher也是继承于ViewSwitcher,不同设置饿ViewFactory的makeView()返回的是TextView组件,可以像Button那样设置点击事件,在xml文件设置OnClick:
package peng.liu.test;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
TextSwitcher textSwitcher;
private String[] strs = new String[]{
"java","python","xml","html"
};
int currentStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView text = new TextView(MainActivity.this);
text.setTextSize(40);
text.setTextColor(Color.MAGENTA);
return text;
}
});
next(null);
}
public void next(View view){
textSwitcher.setText(strs[currentStr++%strs.length]);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inAnimation="@anim/slide_in"
android:outAnimation="@anim/slide_out"
android:onClick="next"
android:id="@+id/textSwitcher"/>
</LinearLayout>
Android的ImageSwitcher和TextSw-android学习之旅(三十四)
标签:android
原文地址:http://blog.csdn.net/lpjishu/article/details/46357641