当前位置:操作系统 > 安卓/Android >>

android Launcher源码解析:DeleteZone

DeleteZone是launcher中比较简单的一部分,其操作为


长按桌面上某个图标后,会出现如下内容:1)dock栏消失;2)原dock栏位置出现一个垃圾箱图案;
将该图标拖动到垃圾箱位置后,会发现如下内容:1)垃圾箱图标变为打开;2)垃圾箱周围出现一片红色区域;3)图标变为红色;
将该图标放到垃圾箱位置后,该图标会被从桌面中删除;
        查看DeleteZone的源码,只要搞清楚了以上3个步骤是如何实现的,那么就理解了他的代码了。


1)构造函数
[java]
public DeleteZone(Context context, AttributeSet attrs) { 
        this(context, attrs, 0); 
    } 
 
    public DeleteZone(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
         
        final int srcColor = context.getResources().getColor(R.color.delete_color_filter); 
        mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP)); 
 
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0); 
        mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL); 
        a.recycle(); 
    } 
 
    @Override 
    protected void onFinishInflate() { 
        super.onFinishInflate(); 
        mTransition = (TransitionDrawable) getDrawable(); 
    } 

public DeleteZone(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
       
        final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
        mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
        mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTransition = (TransitionDrawable) getDrawable();
    }        这里主要完成三项工作:1)构造对象;2)建立红色区域的滤镜颜色为红色;3)判断方向,这个是在launcher.xml中设置的方向。
[html]
<com.xuxm.demo.launcher.DeleteZone 
    android:id="@+id/delete_zone" 
    android:layout_width="@dimen/delete_zone_size" 
    android:layout_height="@dimen/delete_zone_size" 
    android:paddingTop="@dimen/delete_zone_padding" 
    android:layout_gravity="top|center_horizontal" 
 
    android:scaleType="center" 
    <STRONG>android:src="@drawable/delete_zone_selector"</STRONG> 
    android:visibility="invisible" 
   <STRONG> launcher:direction="horizontal"</STRONG> 
    /> 

    <com.xuxm.demo.launcher.DeleteZone
        android:id="@+id/delete_zone"
        android:layout_width="@dimen/delete_zone_size"
        android:layout_height="@dimen/delete_zone_size"
        android:paddingTop="@dimen/delete_zone_padding"
        android:layout_gravity="top|center_horizontal"

        android:scaleType="center"
        android:src="@drawable/delete_zone_selector"
        android:visibility="invisible"
        launcher:direction="horizontal"
        />         同时需要注意到这里的图片并不是一张图片,而是一个TransitionDrawable,其内容为:
[html]
<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/trashcan"  /> 
    <item android:drawable="@drawable/trashcan_hover"  /> 
</transition> 

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/trashcan"  />
    <item android:drawable="@drawable/trashcan_hover"  />
</transition>
          这里,他可以动态切换显示的图片,这也是你看到的垃圾箱关闭与打开两种显示方式的根源。其实现在这几个函数中实现:
[java]
public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, 
         DragView dragView, Object dragInfo) { 
     mTransition.reverseTransition(TRANSITION_DURATION); 
     dragView.setPaint(mTrashPaint); 
 } 
 
 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, 
         DragView dragView, Object dragInfo) { 
 } 
 
 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, 
         DragView dragView, Object dragInfo) { 
     mTransition.reverseTransition(TRANSITION_DURATION); 
     dragView.setPaint(null); 
 } 

   public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        mTransition.reverseTransition(TRANSITION_DURATION);
        dragView.setPaint(mTrashPaint);
    }

    public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
    }

 &nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,