您的位置:首页 > 移动开发 > Android开发

Android开发-自定义View-AndroidStudio(十一)有趣的BUG

2016-12-22 14:03 513 查看
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~

老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
下面这个BUG是同时使用scrollBy和MotionEvent.ACTION_MOVE导致的:
是在我练习的时候写出来的BUG,如果加上临界值的一些判断,可以实现一些有趣的效果:





MainActivity.java:
package com.iwanghang.scrolltest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MyImageView.java:
package com.iwanghang.scrolltest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class MyImageView extends View {

private GestureDetector detector; // 定义手势识别类
private Bitmap image;
private int lastX;
private int lastY;

public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);

// 使用系统工具,获取属性
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyAttributeView);
for (int i = 0; i <typedArray.getIndexCount() ; i++) {
int index = typedArray.getIndex(i);

switch (index){
case R.styleable.MyAttributeView_my_image:
Drawable drawable = typedArray.getDrawable(index);
BitmapDrawable drawable1 = (BitmapDrawable) drawable;
image = drawable1.getBitmap();
break;
}
}
// 记得回收
typedArray.recycle();

initView(context); // 实例化手势识别类
}

/**
* 实例化手势识别类
*/
private void initView(Context context) {
detector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollBy((int)distanceX, (int)distanceY); // 可以上下左右滑动
return true;
}
});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
// 把时间传递给手势识别器
detector.onTouchEvent(event);

//获取到手指处的横坐标和纵坐标
int x = (int) event.getX();
int y = (int) event.getY();

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
//计算移动的距离
int offX = x - lastX;
int offY = y - lastY;
//调用layout方法来重新放置它的位置
layout(getLeft()+offX, getTop()+offY, getRight()+offX, getBottom()+offY);
break;
}

return true;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawBitmap(image,null, new Rect(0,0,100,100),null);
}
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyAttributeView">
<attr name="my_image" format="reference|color"/>
</declare-styleable>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:iwanghang="http://schemas.android.com/apk/res-auto"
tools:context="com.iwanghang.scrolltest.MainActivity">

<TextView
android:textColor="#ff0000"
android:textSize="20sp"
android:text="见证奇迹的时候到了......"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<com.iwanghang.scrolltest.MyImageView
android:id="@+id/iv_main"
android:background="@drawable/bg_red"
iwanghang:my_image="@mipmap/ic_launcher"
android:layout_width="200dp"
android:layout_height="200dp" />

</RelativeLayout>


转载请注明出处:http://blog.csdn.net/iwanghang/

欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式



微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: