Android 应用开发笔记 - 拖动效果(Gallery)
![]() | ![]() | |
![]() | 新建一View,清单如下: view_gallery.xml [html] <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</LinearLayout>
在面板拖拽图标,然后更改相关属性(Properties),如下: [html] <Gallery android:id="@+id/gallery02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp"/> <Gallery android:id="@+id/gallery02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp"/>
添加一按钮,如下: [html] v <Button android:id="@+id/btnReturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="166dp" android:text="@string/btn1Caption" /> <Button android:id="@+id/btnReturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="166dp" android:text="@string/btn1Caption" />
整体布局效果如下: 图片:20120915021046285.png
把“Push me”按钮,更改为切换按钮。即实现切换功能。相关代码请参考多个View切换!
实现Gallery需要继承BaseAdapter,我们命名为ImgAdapter。 代码清单如下: [java] package com.example.prjandroid;
import android.content.Context; import android.content.res.TypedArray; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView;
public class ImgAdapter extends BaseAdapter {
@Override public int getCount() { // TODO Auto-generated method stub return null; }
@Override public Object getItem(int position) { // TODO Auto-generated method stub return position; }
@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub
return null; }
} package com.example.prjandroid; import android.content.Context; import android.content.res.TypedArray; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImgAdapter extends BaseAdapter {
@Override public int getCount() { // TODO Auto-generated method stub return null; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub
return null; } }
如果想要放图片,必须使用ImageView! 这时,我们把图片放到res/drawable下(建议使用png格式文件)。 然后,再新建一xml文件,提供Gallery的背景。如下: res/attrs.xml [html] <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="galleryThem"> <attr name="android:galleryItemBackground"/> </declare-styleable> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="galleryThem"> <attr name="android:galleryItemBackground"/> </declare-styleable> </resources>
声明一图片ID的数组: [java] // resource draw private int[] resPics = new int[] { R.drawable.emacs1, R.drawable.emacs2, R.drawable.emacs3, R.drawable.emacs4, R.drawable.emacs5, R.drawable.emacs6, R.drawable.emacs7, R.drawable.emacs8, R.drawable.emacs9, R.drawable.emacs10 }; // resource draw private int[] resPics = new int[] { R.drawable.emacs1, R.drawable.emacs2, R.drawable.emacs3, R.drawable.emacs4, R.drawable.emacs5, R.drawable.emacs6, R.drawable.emacs7, R.drawable.emacs8, R.drawable.emacs9, R.drawable.emacs10 };
在getView()方法中使用ImageView:
[java] ImageView imgView = new ImageView(m_context); imgView.setImageResource(resPics[position]); imgView.setScaleType(ImageView.ScaleType.FIT_XY); imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));
imgView.setBackgroundResource(m_galleryItemBackGround);
return imgView; ImageView imgView = new ImageView(m_context); imgView.setImageResource(resPics[position]); imgView.setScaleType(ImageView.ScaleType.FIT_XY); imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));
imgView.setBackgroundResource(m_galleryItemBackGround);
return imgView;
其中,ImageView.ScaleType共八种: 1·ImageView.ScaleType.center:图片位于视图中间,但不执行缩放。 2·ImageView.ScaleType.CENTER_CROP 按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者大于相应的视图的维度 3·ImageView.ScaleType.CENTER_INSIDE按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者小于相应的视图的维度 4·ImageView.ScaleType.FIT_CENTER缩放图片使用center 5·ImageView.ScaleType.FIT_END缩放图片使用END 6·ImageView.ScaleType.FIT_START缩放图片使用START 7·ImageView.ScaleType.FIT_XY缩放图片使用XY 8·ImageView.ScaleType.MATRIX当绘制时使用图片矩阵缩放
还有就是Context相当于windows中的Handle。
我们在构造这个类时,需要Context和初始化Gallery的属性,其相关代码如下: [java] public ImgAdapter(Context context) { // TODO Auto-generated constructor stub m_context = context; TypedArray typeArray = m_context.obtainStyledAttributes(R.styleable.galleryThem); m_galleryItemBackGround = typeArray.getResourceId( R.styleable.galleryThem_android_galleryItemBackground, 0); } public ImgAdapter(Context context) { // TODO Auto-generated constructor stub m_context = context; TypedArray typeArray = m_context.obtainStyledAttributes(R.styleable.galleryThem); m_galleryItemBackGround = typeArray.getResourceId( R.styleable.galleryThem_android_galleryItemBackground, 0); }
基本上这个适配类就是这个样子了。
调用代码和之前讲的Spinner比较像!区别在于Spinner直接就传的this,但是在必须传主类.this(例如MainActivity.this)。
运行效果如下:
图片:20120915021047536.png | |
![]() | ![]() |