• 模仿去哪儿的磁贴效果


    感觉去哪儿的页面做的很不错,很好看,于是想模仿一下。事实上实现还是很easy的。就是按下去的运行缩小动画,抬起的恢复正常状态,这样的效果叫磁贴效果。顾名思义感觉就磁贴一样。

    以下我们来看看效果图:


    以下我们来看看最重要的自己定义代码:

    package com.zqy.qunertext;
    
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.view.MotionEvent;
    import android.view.animation.Animation;
    import android.view.animation.ScaleAnimation;
    import android.widget.FrameLayout;
    
    
    /**
     * 
     * 
     * @author zqy
     * 
     */
    public class HomeMenuButton extends FrameLayout {
    	private boolean isPressed;
    	private ScaleAnimation zoomInAnimation;
    	private ScaleAnimation zoomOutAnimation;
    
    	public HomeMenuButton(Context context) {
    		this(context,null);
    	}
    
    	public HomeMenuButton(Context context, AttributeSet attrs) {
    		super(context, attrs);
    		init();
    	}
    
    	@Override
    	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    	}
    
    	private void init() {
    		/**
    		 * 初始化动画
    		 */
    		zoomInAnimation = new ScaleAnimation(1f, 0.95f, 1f, 0.95f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
    		zoomInAnimation.setFillAfter(true);
    		zoomInAnimation.setDuration(200);
    		zoomOutAnimation = new ScaleAnimation(0.95f, 1f, 0.95f, 1f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
    		zoomOutAnimation.setFillAfter(true);
    		zoomOutAnimation.setDuration(200);
    
    	}
    
    	@Override
    	protected void onDraw(Canvas canvas) {
    		super.onDraw(canvas);
    	}
    
    	private void toNormalState() {
    		isPressed = false;
    		invalidate();
    		startAnimation(zoomOutAnimation);
    	}
    
    	private boolean pointInView(float localX, float localY, float slop) {
    		return localX >= -slop && localY >= -slop && localX < getWidth() + slop
    				&& localY < getHeight() + slop;
    	}
    	
    	@Override
    	public boolean onTouchEvent(MotionEvent event) {
    		switch (event.getAction()) {
    		case MotionEvent.ACTION_DOWN:
    			isPressed = true;//设置true
    			invalidate();//重绘
    			this.startAnimation(zoomInAnimation);//运行动画
    			break;
    		case MotionEvent.ACTION_UP:
    			boolean needPerformClick = isPressed;
    			toNormalState();//正常
    			if (needPerformClick) {
    				performClick();
    			}
    			break;
    			
    		case MotionEvent.ACTION_MOVE:
    			final int x = (int) event.getX();
    			final int y = (int) event.getY();
    			if (!pointInView(x, y, 20)) {
    				toNormalState();
    			}
    			break;
    			
    		case MotionEvent.ACTION_CANCEL:
    		case MotionEvent.ACTION_OUTSIDE:
    			toNormalState();
    			break;
    		}
    		
    		return true;
    		
    	}
    }
    上面代码晚上差点儿就已经完毕了:仅仅有把自己定义的代码写在XML里面就能够了:

    <?xml version="1.0" encoding="utf-8"?

    > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_weight="1" android:orientation="vertical" > <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_ad" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="@drawable/icon_favoable" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_order" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="2" android:background="@drawable/icon_order" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_cloud_manger" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@drawable/icon_favorable_manger" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_setting" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@drawable/icon_setting" > </com.zqy.qunertext.HomeMenuButton> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:layout_weight="1" android:orientation="vertical" > <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_goods_manager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/icon_goods" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_store_net" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="2" android:background="@drawable/icon_store" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_incoming" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="2" android:background="@drawable/icon_money" > </com.zqy.qunertext.HomeMenuButton> <com.zqy.qunertext.HomeMenuButton android:id="@+id/hb_employee_manager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@drawable/icon_manage" > </com.zqy.qunertext.HomeMenuButton> </LinearLayout> </LinearLayout> </LinearLayout>

    OK.大功告成。

    效果还能够:呵呵



    下载地址:






  • 相关阅读:
    「Luogu」2831愤怒的小鸟 (DFS+dp)
    LeetCode习题集
    递归的时间复杂度你真的懂吗?不是所有的二分递归都是logn级别
    [数据结构篇]谈一谈优先队列吧!
    论文爱好者(我不是)的福利
    Python 读微博留言进行情感分析(文本分类)
    python 多进程中的p.apply_async()
    记录本科论文开题报告修改过程
    KMP字符串匹配算法
    Pandas Timedelta
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5109882.html
Copyright © 2020-2023  润新知