你曾经被iphone用手指在屏幕划来花去拖动相片吸引吗?在Android同样能够实现,而且不是什么难事。

这里需要用到android.content.Context;android.widget.BaseAdapter;android.wiget.ImageView;等。

android.content.Context在Activity中类似于一张Canvas画布,能够随时处理或覆盖它。Context与Intent一样是android.content的子类。

本实例通过在layout中放置Gallery对象,然后通过android.widget.BaseAdapter容器放置Gallery所需要的图片,本案例使用系统默认的ICON图片。

1.如图,拖拽Gallery控件到layout中,ID设置为myGallery1

image
image

查看res/layout/main.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:layout_width="fill_parent"      android:layout_height="wrap_content"      android:id="@+id/myGallery1"></Gallery></LinearLayout>

src/EX03_15.java

package gphone.ex03_15;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.Gallery;import android.widget.ImageView;public class EX03_15 extends Activity {private Gallery myGallery1=null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                myGallery1=(Gallery)findViewById(R.id.myGallery1);        myGallery1.setAdapter(new ImageAdapter(this));    }    public class ImageAdapter extends BaseAdapter{    private Context myContext;    //使用android.R.drawable里系统默认图片作为图片源    private int[] myImageIds={    android.R.drawable.btn_minus,    android.R.drawable.btn_radio,    android.R.drawable.ic_lock_idle_low_battery,    android.R.drawable.ic_menu_camera,    android.R.drawable.btn_minus,    android.R.drawable.btn_radio,    android.R.drawable.ic_lock_idle_low_battery,    android.R.drawable.ic_menu_camera,android.R.drawable.btn_minus,    android.R.drawable.btn_radio,    android.R.drawable.ic_lock_idle_low_battery,    android.R.drawable.ic_menu_camera    };    public ImageAdapter(Context c){    this.myContext=c;    }        @Override    public int getCount() {    // TODO Auto-generated method stub    return this.myImageIds.length;    }    @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) {    // 创建ImageView    ImageView i=new ImageView(this.myContext);        i.setImageResource(this.myImageIds[position]);    i.setScaleType(ImageView.ScaleType.FIT_XY);    //设置ImageView对象的宽高,单位为dip    i.setLayoutParams(new Gallery.LayoutParams(120, 120));        return i;    }    //根据中心点位移量 利用getScale返回views的大小(0.0f-1.0f)    public float getScale(boolean focused,int offset){    return Math.max(0,1.0f/(float)Math.pow(2,Math.abs(offset)));    }        }}

运行效果

image

更多相关文章

  1. Android根据屏幕宽度缩放图片
  2. Android Activity之间传递图片(Bitmap)的方法
  3. Android中WebView获取网页中标题 ,内容, 图片的方法
  4. android 4.0 以上平台选择图片报错Attempted to access a cursor
  5. android Java BASE64编码和解码二:图片的编码和解码
  6. Android设置Selector不同状态下颜色及图片
  7. android 5.1拍照后图片镜像处理

随机推荐

  1. uni-app系统目录文件上传(非只图片和视频)
  2. Android(安卓)App优化, 要怎么做?
  3. Qt on Android:使用JNI与第三方jar包
  4. Android(安卓)从程序启动startActivity到
  5. Android(安卓)Toast优化--通过自定义布局
  6. Android:创建可穿戴应用 - 打包发布
  7. Android(安卓)获取经纬度的服务
  8. Android~Fragment的替代方案
  9. android:Adb connection Error:远程主机强
  10. android学习笔记1——webview相关