• 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

  • 相关阅读:
    C# 根据当前登录的用户信息,显示数据库中登录用户的照片
    C# 根据当前系统时间,显示 Good Morning/Afternoon/Evening
    C# 根据用户性别,在身份证号输入框后 面显示问候信息,格式为:“Mrs./Mr. Name”。
    C# 窗体登录,按照他们的角色跳转到不同的主界面
    C# 实现模拟下载功能,被下载的文件存储在本地
    C# 鼠标移动到图片上显示提示文字
    C# 隔一段时间自动刷新弹框(医院病人超时提醒)
    C# winform 点击窗体内不同按钮显示不同的文字
    gulp的安装和使用
    es6 面向对象
  • 原文地址:https://www.cnblogs.com/AlexCheng/p/2120040.html
Copyright © 2020-2023  润新知