Android 自定义CoolImageView实现QQ首页背景图片动画效果

Android 第三方RoundedImageView设置各种圆形、方形头像

一.自定义ImageView

1.效果图:

2.自定义ImageView

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;


/**
 * Created by hzhuqi on 2016/11/23.
 */

public class CoolImageView extends ImageView {


    private Drawable mDrawable;
    private  int mLeft=0;
    private  int mTop=0;
    private  int mSpeed=2;
    private boolean isSetVerticalMove;
    private boolean isMoveLeft;
    private boolean isMoveUp;
    private Handler mHandler;
    private int mCanvasBgSize;
    public CoolImageView(Context context) {
        super(context);
    }

    public CoolImageView(Context context, AttributeSet attrs) {
         super(context, attrs);
         setUp(context,attrs);
    }

    public CoolImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setUp(context,attrs);
    }

    private void setUp(Context context, AttributeSet attrs)
    {
        TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.CoolImageView);
        String direction=a.getString(R.styleable.CoolImageView_direction);
        if(direction==null)
        {
            throw new RuntimeException("You don't set direction properties,If you don't want to do that." +
                    "You can use ordinary ImageView instead");
        }else if(direction.equals("vertical"))
        {
            isSetVerticalMove=true;
        }
        else if(direction.equals("horizontal"));
        else
        {
            throw new RuntimeException("Direction attribute set is not valid,It is only allowed to set to vertical or horizontal");
        }
        mDrawable=getDrawable();
        mHandler=new MoveHandler();
        mHandler.sendEmptyMessageDelayed(1, 220L);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      //  if(direction.equals("vertical"))
      if(isSetVerticalMove)
        {
            canvas.translate(0.0F,mTop);
        }else
        {
            canvas.translate(mLeft, 0.0F);
        }
        mDrawable.draw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
      //  if(direction.equals("vertical"))//如果是上下移动
        if(isSetVerticalMove)
        {
            mCanvasBgSize=getMeasuredHeight()*3/2;
            mDrawable.setBounds(0,0,getMeasuredWidth(),mCanvasBgSize);
        }else
        {
            mCanvasBgSize=getMeasuredWidth()*3/2;
            mDrawable.setBounds(0,0,mCanvasBgSize,getMeasuredHeight());
        }
    }

    private class MoveHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
          //  if(direction.equals("vertical"))//如果用户设置的是上下滚动
            if(isSetVerticalMove)
            {
                if(isMoveUp)
                {
                    if(mTop<=getMeasuredHeight()-mCanvasBgSize)//此时表示移到了最up的位置
                    {
                        mTop+=mSpeed;
                        isMoveUp = false;
                    }else//继续下移
                    {
                        mTop-=mSpeed;
                    }
                }
                else
                {
                    if(mTop==0)//此时表示移动到了最down,此时图片的up侧应该与屏幕up侧对齐,即坐标值为0
                    {
                        mTop-=mSpeed;
                        isMoveUp = true;//图片已经移动到了最down侧,需要修改其移动方向为up
                    }else
                    {

                        mTop+=mSpeed;//继续下移
                    }
                }
            }
            else
            {
                if(isMoveLeft)//向左移动
                {
                   //   Log.i("translate ","left");
                    if(mLeft<=getMeasuredWidth()-mCanvasBgSize)//此时表示移到了最左侧的位置
                    {
                        mLeft+=mSpeed;
                        isMoveLeft = false;
                    }else//继续左移
                    {
                        mLeft-=mSpeed;
                    }

                }else {
                    if (mLeft == 0)//此时表示移动到了最右侧,此时图片的左侧应该与屏幕左侧对齐,即坐标值为0
                    {
                        mLeft-=mSpeed;
                        isMoveLeft = true;//图片已经移动到了最右侧,需要修改其移动方向为向左
                    } else {
                        mLeft+=mSpeed;//继续右移
                    }
                }
            }
            invalidate();
            mHandler.sendEmptyMessageDelayed(1, 22);
        }
    }
}

3.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sgf.coolimagview.MainActivity">

    <com.example.sgf.coolimagview.CoolImageView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:src="@mipmap/bg_one"
        app:direction="horizontal"

       />

    <com.example.sgf.coolimagview.CoolImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/bg_two"
        app:direction="vertical"
        />
</LinearLayout>

4.attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CoolImageView">
        <attr name="direction" format="string"/>
    </declare-styleable>
</resources>

只需要布局设置就可以,代码不需要设置。

 

相关源码:

https://github.com/HuTianQi/CoolImageView

https://github.com/open-android/PhotoImageView   实现图片手势缩放

https://github.com/open-android/uCrop    图片裁剪

https://github.com/open-android/GuideDialog   实现ofo小黄车的引导界面

https://github.com/open-android/DragFooterView   实现向左拖拽跳转详情页

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值