- 浏览: 472142 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
bewithme:
我把一个透明背景的PNG画到了另外一张图片上,为啥透明背景就变 ...
Java绘制PNG图片时的黑色背景问题 -
davidwkj:
Y坐标的公式不对。算出来的值是错的!!!!!!!!!!!!!! ...
两条线段是否相交,计算交点公式。 -
zhang9181156:
茅塞顿开哈,感谢
android--相机开发 -
leecinwell:
哈
Android条用照相机,实现可剪切功能 -
life_菜鸟飞:
非常感谢作者这么详细的讲解,自己也动手实践了一下,发现前面几个 ...
android--相机开发
系出名门Android(8) - 控件(View)之TextSwitcher, Gallery, ImageSwitcher, GridView, ListVi
- 博客分类:
- Android
系出名门Android(8) - 控件(View)之TextSwitcher, Gallery, ImageSwitcher, GridView, ListView, ExpandableList
作者:webabcd
介绍
在 Android 中使用各种控件(View)
TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果)
Gallery - 缩略图浏览器控件
ImageSwitcher - 图片转换器控件(改变图片时增加一些动画效果)
GridView - 网格控件
ListView - 列表控件
ExpandableList - 支持展开/收缩功能的列表控件
1、TextSwitcher 的 Demo
textswitcher.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnChange" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="改变文字" />
<!--
TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果)
-->
<TextSwitcher android:id="@+id/textSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
_TextSwitcher.java
代码
package com.webabcd.view;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class _TextSwitcher extends Activity implements ViewSwitcher.ViewFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
setTitle("TextSwithcer");
final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
// 指定转换器的 ViewSwitcher.ViewFactory
switcher.setFactory(this);
// 设置淡入和淡出的动画效果
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
// 单击一次按钮改变一次文字
Button btnChange = (Button) this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重写 ViewSwitcher.ViewFactory 的 makeView(),返回一个 View
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
}
2、Gallery 的 Demo
gallery.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--
Gallery - 缩略图浏览器控件
spacing - 缩略图列表中各个缩略图之间的间距
-->
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="20px" />
</LinearLayout>
_Gallery.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Gallery.LayoutParams;
public class _Gallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gallery);
setTitle("Gallery");
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 为缩略图浏览器指定一个适配器
gallery.setAdapter(new ImageAdapter(this));
// 响应 在缩略图列表上选中某个缩略图后的 事件
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(_Gallery.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
// 继承 BaseAdapter 用以实现自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
// 需要显示的图片集合
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}
3、ImageSwitcher 的 Demo
imageswitcher.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="20px" />
<!--
ImageSwitcher - 图片转换器控件(改变图片时增加一些动画效果)
-->
<ImageSwitcher android:id="@+id/imageSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
_ImageSwitcher.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
// 图片转换器的使用基本同文字转换器
// 以下是一个用 ImageSwitcher + Gallery 实现的经典的图片浏览器的 Demo
public class _ImageSwitcher extends Activity implements
ViewSwitcher.ViewFactory {
private ImageSwitcher mSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.imageswithcer);
setTitle("ImageSwithcer");
mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
private Integer[] mImageIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setMinimumHeight(200);
image.setMinimumWidth(200);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return image;
}
}
4、GridView 的 Demo
gridview.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<!--
GridView - 网格控件
numColumns="auto_fit" - 列数自适应
stretchMode - 缩放模式(stretchMode="columnWidth" - 缩放与列宽大小同步)
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10px"
android:verticalSpacing="10px" android:horizontalSpacing="10px"
android:numColumns="auto_fit" android:columnWidth="60px"
android:stretchMode="columnWidth" android:gravity="center">
</GridView>
_GridView.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class _GridView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gridview);
setTitle("GridView");
GridView gridView = (GridView) findViewById(R.id.gridView);
// 指定网格控件的适配器为自定义的图片适配器
gridView.setAdapter(new ImageAdapter(this));
}
// 自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(48, 48));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// 网格控件所需图片数据的数据源
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}
}
5、ListView 的 Demo
main_list_adapter.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<!--
自定义列表适配器的 layout
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16sp">
</TextView>
</LinearLayout>
MainListAdapter.java
代码
package com.webabcd.view;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
// 继承 BaseAdapter 以实现自定义的列表适配器
public class MainListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> mData;
public MainListAdapter(Context context, List<String> data) {
mInflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
// 指定一个 layout 作为自定义列表适配器的 layout
convertView = mInflater.inflate(R.layout.main_list_adapter, null);
text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(text);
} else {
text = (TextView) convertView.getTag();
}
String mItem = mData.get(position);
text.setText(mItem);
return convertView;
}
}
Main.java
代码
package com.webabcd.view;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
// 此处要继承 ListActivity ,用以实现 ListView 的功能
public class Main extends ListActivity {
private List<String> mData;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.main);
mData = getData();
// 使用自定义的列表适配器来展现数据
MainListAdapter adapter = new MainListAdapter(this, mData);
// 如需使用系统内置的列表适配器,则可以使用类似如下的方法
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, mData);
this.setListAdapter(adapter);
}
// ListView 的数据源
private List<String> getData() {
List<String> items = new ArrayList<String>();
items.add("TextView");
items.add("Button");
items.add("ImageButton");
items.add("ImageView");
items.add("CheckBox");
items.add("RadioButton");
items.add("AnalogClock");
items.add("DigitalClock");
items.add("DatePicker");
items.add("TimePicker");
items.add("ToggleButton");
items.add("EditText");
items.add("ProgressBar");
items.add("SeekBar");
items.add("AutoCompleteTextView");
items.add("MultiAutoCompleteTextView");
items.add("ZoomControls");
items.add("Include");
items.add("VideoView");
items.add("WebView");
items.add("RatingBar");
items.add("Tab");
items.add("Spinner");
items.add("Chronometer");
items.add("ScrollView");
items.add("TextSwitcher");
items.add("ListView");
items.add("Gallery");
items.add("ImageSwitcher");
items.add("GridView");
items.add("ExpandableList");
return items;
}
// ListView 中某项被选中后的逻辑
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent();
intent.setClassName(this, "com.webabcd.view._" + mData.get(position));
startActivityForResult(intent, 0);
}
}
6、ExpandableList 的 Demo
_ExpandableList.java
代码
package com.webabcd.view;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
// ExpandableList - 可展开/收缩列表
// 继承 ExpandableListActivity 以实现列表的可展开/收缩的功能
public class _ExpandableList extends ExpandableListActivity {
private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("ExpandableList");
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(this.getExpandableListView());
}
// 为列表的每一项创建上下文菜单(即长按后呼出的菜单)
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("ContextMenu");
menu.add(0, 0, 0, "ContextMenu");
}
// 单击上下文菜单后的逻辑
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + " - Group Index: " + groupPos + " Child Index: " + childPos, Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + " - Group Index: " + groupPos, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// 父列表数据
private String[] groups =
{
"group1",
"group2",
"group3",
"group4"
};
// 子列表数据
private String[][] children =
{
{ "child1" },
{ "child1", "child2" },
{ "child1", "child2", "child3" },
{ "child1", "child2", "child3", "child4" }
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
// 取子列表中的某一项的 View
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// 取父列表中的某一项的 View
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
// 获取某一项的 View 的逻辑
private TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 48);
TextView textView = new TextView(_ExpandableList.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(32, 0, 0, 0);
return textView;
}
}
}
作者:webabcd
介绍
在 Android 中使用各种控件(View)
TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果)
Gallery - 缩略图浏览器控件
ImageSwitcher - 图片转换器控件(改变图片时增加一些动画效果)
GridView - 网格控件
ListView - 列表控件
ExpandableList - 支持展开/收缩功能的列表控件
1、TextSwitcher 的 Demo
textswitcher.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnChange" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="改变文字" />
<!--
TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果)
-->
<TextSwitcher android:id="@+id/textSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
_TextSwitcher.java
代码
package com.webabcd.view;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class _TextSwitcher extends Activity implements ViewSwitcher.ViewFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
setTitle("TextSwithcer");
final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
// 指定转换器的 ViewSwitcher.ViewFactory
switcher.setFactory(this);
// 设置淡入和淡出的动画效果
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
// 单击一次按钮改变一次文字
Button btnChange = (Button) this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重写 ViewSwitcher.ViewFactory 的 makeView(),返回一个 View
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
}
2、Gallery 的 Demo
gallery.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--
Gallery - 缩略图浏览器控件
spacing - 缩略图列表中各个缩略图之间的间距
-->
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="20px" />
</LinearLayout>
_Gallery.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Gallery.LayoutParams;
public class _Gallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gallery);
setTitle("Gallery");
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 为缩略图浏览器指定一个适配器
gallery.setAdapter(new ImageAdapter(this));
// 响应 在缩略图列表上选中某个缩略图后的 事件
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(_Gallery.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
// 继承 BaseAdapter 用以实现自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
// 需要显示的图片集合
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}
3、ImageSwitcher 的 Demo
imageswitcher.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:spacing="20px" />
<!--
ImageSwitcher - 图片转换器控件(改变图片时增加一些动画效果)
-->
<ImageSwitcher android:id="@+id/imageSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
_ImageSwitcher.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
// 图片转换器的使用基本同文字转换器
// 以下是一个用 ImageSwitcher + Gallery 实现的经典的图片浏览器的 Demo
public class _ImageSwitcher extends Activity implements
ViewSwitcher.ViewFactory {
private ImageSwitcher mSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.imageswithcer);
setTitle("ImageSwithcer");
mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
}
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
private Integer[] mImageIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setMinimumHeight(200);
image.setMinimumWidth(200);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return image;
}
}
4、GridView 的 Demo
gridview.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<!--
GridView - 网格控件
numColumns="auto_fit" - 列数自适应
stretchMode - 缩放模式(stretchMode="columnWidth" - 缩放与列宽大小同步)
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10px"
android:verticalSpacing="10px" android:horizontalSpacing="10px"
android:numColumns="auto_fit" android:columnWidth="60px"
android:stretchMode="columnWidth" android:gravity="center">
</GridView>
_GridView.java
代码
package com.webabcd.view;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class _GridView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gridview);
setTitle("GridView");
GridView gridView = (GridView) findViewById(R.id.gridView);
// 指定网格控件的适配器为自定义的图片适配器
gridView.setAdapter(new ImageAdapter(this));
}
// 自定义的图片适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(48, 48));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// 网格控件所需图片数据的数据源
private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };
}
}
5、ListView 的 Demo
main_list_adapter.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<!--
自定义列表适配器的 layout
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16sp">
</TextView>
</LinearLayout>
MainListAdapter.java
代码
package com.webabcd.view;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
// 继承 BaseAdapter 以实现自定义的列表适配器
public class MainListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> mData;
public MainListAdapter(Context context, List<String> data) {
mInflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
// 指定一个 layout 作为自定义列表适配器的 layout
convertView = mInflater.inflate(R.layout.main_list_adapter, null);
text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(text);
} else {
text = (TextView) convertView.getTag();
}
String mItem = mData.get(position);
text.setText(mItem);
return convertView;
}
}
Main.java
代码
package com.webabcd.view;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
// 此处要继承 ListActivity ,用以实现 ListView 的功能
public class Main extends ListActivity {
private List<String> mData;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.main);
mData = getData();
// 使用自定义的列表适配器来展现数据
MainListAdapter adapter = new MainListAdapter(this, mData);
// 如需使用系统内置的列表适配器,则可以使用类似如下的方法
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, mData);
this.setListAdapter(adapter);
}
// ListView 的数据源
private List<String> getData() {
List<String> items = new ArrayList<String>();
items.add("TextView");
items.add("Button");
items.add("ImageButton");
items.add("ImageView");
items.add("CheckBox");
items.add("RadioButton");
items.add("AnalogClock");
items.add("DigitalClock");
items.add("DatePicker");
items.add("TimePicker");
items.add("ToggleButton");
items.add("EditText");
items.add("ProgressBar");
items.add("SeekBar");
items.add("AutoCompleteTextView");
items.add("MultiAutoCompleteTextView");
items.add("ZoomControls");
items.add("Include");
items.add("VideoView");
items.add("WebView");
items.add("RatingBar");
items.add("Tab");
items.add("Spinner");
items.add("Chronometer");
items.add("ScrollView");
items.add("TextSwitcher");
items.add("ListView");
items.add("Gallery");
items.add("ImageSwitcher");
items.add("GridView");
items.add("ExpandableList");
return items;
}
// ListView 中某项被选中后的逻辑
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent();
intent.setClassName(this, "com.webabcd.view._" + mData.get(position));
startActivityForResult(intent, 0);
}
}
6、ExpandableList 的 Demo
_ExpandableList.java
代码
package com.webabcd.view;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
// ExpandableList - 可展开/收缩列表
// 继承 ExpandableListActivity 以实现列表的可展开/收缩的功能
public class _ExpandableList extends ExpandableListActivity {
private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("ExpandableList");
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(this.getExpandableListView());
}
// 为列表的每一项创建上下文菜单(即长按后呼出的菜单)
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("ContextMenu");
menu.add(0, 0, 0, "ContextMenu");
}
// 单击上下文菜单后的逻辑
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + " - Group Index: " + groupPos + " Child Index: " + childPos, Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + " - Group Index: " + groupPos, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// 父列表数据
private String[] groups =
{
"group1",
"group2",
"group3",
"group4"
};
// 子列表数据
private String[][] children =
{
{ "child1" },
{ "child1", "child2" },
{ "child1", "child2", "child3" },
{ "child1", "child2", "child3", "child4" }
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
// 取子列表中的某一项的 View
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// 取父列表中的某一项的 View
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
// 获取某一项的 View 的逻辑
private TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 48);
TextView textView = new TextView(_ExpandableList.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(32, 0, 0, 0);
return textView;
}
}
}
发表评论
-
浅析android应用增量升级
2013-05-29 13:31 2731背景 随着android应用体积的不断增大 ... -
Activity是如何生成一个 view 的
2013-05-16 11:09 1369Activity是一个工人,它来控制Window;Windo ... -
Android 获取新浪天气预报数据
2012-12-07 13:07 3084新浪天气预报地址: http://php.weathe ... -
Android连接网络问题
2012-11-23 11:14 2043一、检查网络是否连接 方法一、 publ ... -
Android异步下载图片并且缓存图片到本地
2012-09-06 16:21 1942转来的,以备不时之需!! 在Android开发中 ... -
Android条用照相机,实现可剪切功能
2012-08-12 12:35 4599public class CameraActivity ... -
android--相机开发
2012-08-10 08:17 11613在android中应用相机功能,一般有两种:一种是直接调 ... -
15个令人震惊的HTML5效果
2012-07-04 10:09 136415个令人震惊的HTML5效果 除非你一直 ... -
Android 读写文件的N种写法
2012-07-04 09:54 1115Android 读写文件的N种写法(待续。。。) ... -
如何干掉360(付代码)
2012-06-29 16:09 1370最近360很凶,有推送的,有桌面图标的,全部会被提示有风 ... -
Cocos2d-x跨Android&iOS平台开发入门
2012-06-26 10:22 1566好吧,确实存在这 ... -
解决Android加载图片时内存溢出的问题
2012-06-18 13:52 46355尽量不要使用setImageBitmap或setImag ... -
android 代码 混淆
2012-06-04 10:22 20761、得到 classes.dex文件;直接用你机器上的 ... -
android代码混淆是出现Conversion to Dalvik format failed with error 1的解决方法
2012-06-04 10:18 4499最近做android项目,给代码混淆时遇到问题,在项目加入混淆 ... -
Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
2012-03-20 16:44 1897一、Surface Surface就是“表面”的意思。 ... -
Re-installation failed解决方法
2011-12-27 22:49 4854我的方法:去模拟器卸载上次安装的*.apk ... -
Failed to install *.apk on device 'emulator-5554': timeout
2011-12-20 00:31 9324错误提示: Failed to inst ... -
android 中的 Bitmap
2011-12-20 00:32 3959在Android SDK中说明可以支持的图片格式如下:p ... -
Android 小球重力感应实现
2011-12-16 20:49 1674重力感应主要是依靠手机的加速度传感器(acceleromete ... -
Android中资源文件夹res/raw和assets的使用
2011-12-16 20:37 9566*res/raw和assets的相同点: 1.两者目录 ...
相关推荐
在Android开发中,`TextSwitcher`和`ImageSwitcher`是两种非常实用的视图切换组件,主要用于在界面上动态地展示文本或图像。它们都继承自`ViewSwitcher`类,提供了一种平滑过渡的效果,使得界面在内容更新时更加流畅...
android-TextSwitcher-master.zip对你的学习会有帮助的
根据提供的文件信息,我们可以总结出以下几个关键的知识点: ### 1. ImageView 控件详解 **简介:** `ImageView` 是 Android UI 开发中一个非常重要的控件,主要用于显示图像资源。它可以用来展示静态图片,也可以...
ImageSwitcher和TextSwitcher是Android SDK提供的一对控件,专门用于实现这样的动态切换效果。下面我们将深入探讨这两个控件的工作原理、使用方法以及实际应用中的注意事项。 首先,ImageSwitcher是用于在两个...
在Android开发中,ViewSwitcher是一个非常实用的控件,它属于ViewAnimator家族的一部分,用于在两个或更多视图之间进行切换。这个实例是关于如何使用ViewSwitcher的子类ImageSwitcher和TextSwitcher来实现动态更换...
在Android开发中,TextSwitcher是一个非常实用的控件,主要用于在界面上动态地切换文本内容,常用于显示一些简短的更新信息或者状态提示。它继承自ViewSwitcher类,具备了动画效果,使得文本的切换更加平滑自然。本...
在Android开发中,ViewSwitcher、ImageSwitcher和TextSwitcher是三种用于切换视图的控件,它们属于ViewFlipper家族,常用于实现动画效果的视图切换。这三个组件在用户界面设计中扮演着重要角色,特别是在创建动态、...
本篇文章将深入探讨如何在Android中实现这样一个功能,主要使用TextSwitcher控件。 TextSwitcher是Android SDK提供的一种特殊的View,它主要用于在两个文本之间平滑地切换。这个控件特别适合用于动态更新内容,例如...
第六章“切换类TextSwitcher和ImageSwitcher”则介绍了用于动态切换文本和图像的控件,适用于制作简单的动画效果或信息展示。 第七章“下拉框Spinner控件”讲述了Spinner的使用,包括数据绑定、监听选择变化,使得...
在“演示Android中的文字和图片特效控件的使用”资源中,讲是的TextSwitcher控件、Gallery控件和ImageSwitcher控件的特效实现。而这个资源演示的是android的动画编程,本示例针对图片和ListView控件实现动画。本人...
在Android开发中,`TextSwitcher` 是一个用于创建文本切换动画效果的视图组件,常用于实现广告条或信息提示区域的文字上下左右滚动效果。它继承自`ViewSwitcher`,并内置了平滑过渡的动画,使得文本在切换时更加自然...
在Android开发中,TextSwitcher是一个非常实用的控件,常用于实现文本的动态切换效果,比如我们在京东、淘宝的搜索框中看到的那种滚动小广播。这个“TextSwitcher垂直滚动文字广告demo”是一个示例项目,展示了如何...
在Android开发中,`TextSwitcher`是一个非常实用的视图组件,主要用于在界面上动态地切换文本内容,常用于创建简单的动画效果,使界面更具交互性和吸引力。本实例"Android TextSwitcher Demo"旨在展示如何有效地使用...
在Android开发过程中,`TextSwitcher`类作为一个特殊的控件,被设计用于动态地改变界面上显示的文本,并支持动画效果,从而使得用户界面更加生动有趣。`TextSwitcher`继承自`ViewSwitcher`类,它仅接受`TextView`...
在Android开发中,TextSwitcher是一个非常实用的控件,它是ViewSwitcher的子类,主要用来实现文本的自动切换效果。TextSwitcher继承了ViewSwitcher的所有特性,包括动画切换效果,使得文本的显示更加生动有趣。在...
`TextSwitcher`是Android平台中的一个特殊视图组件,主要设计用于在界面上动态地切换文本内容。它属于`ViewSwitcher`家族的一部分,这些组件通常用来在用户交互后或者按照一定时间间隔自动切换不同的视图。在`...
### Android控件及布局的使用知识点详解 #### 第一章:Android基础 ##### 1.1 Android开发环境搭建 - **安装JDK (Java Development Kit)**:Java是Android开发的基础语言,因此首先需要安装JDK。可以从Oracle官网...
总结,《50个Android黑客技巧 - Hack5》主要围绕TextSwitcher和动画技术展开,通过理解和实践这些技巧,开发者可以更好地利用Android系统提供的工具,创造出更具吸引力和用户体验的移动应用。同时,对动画性能的优化...
在Android开发中,UI元素的动态效果是提升用户体验的关键因素之一。`TextView`和`TextSwitcher`都是Android SDK提供的重要组件,它们用于显示文本信息。在这个话题中,我们将深入探讨如何利用`TextView`实现跑马灯...