Android拖动相片特效

你曾经被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

原文地址:https://www.cnblogs.com/AlexCheng/p/2120040.html