android 拖动圆形控件 滑动到屏幕边缘随机改变颜色
2016-11-11 23:53
567 查看
下面是activity的代码:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener { Button button; GradientDrawable drawable; int screenWidth = 0; int screenHeight = 0; int lastX = 0; int lastY = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 50; button = (Button) findViewById(R.id.btn_hh); drawable = (GradientDrawable) button.getBackground(); button.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { // 随机改变颜色 Random random = new Random(); int ranColor = 0; int ea=event.getAction(); switch(ea){ case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; /** * layout(l,t,r,b) * l Left position, relative to parent t Top position, relative to parent r Right position, relative to parent b Bottom position, relative to parent * */ case MotionEvent.ACTION_MOVE: int dx =(int)event.getRawX() - lastX; int dy =(int)event.getRawY() - lastY; int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if(left < 0){ left = 0; right = left + v.getWidth(); ranColor = 0xff000000 | random.nextInt(0x00ffffff); drawable.setColor(ranColor); } if(right > screenWidth){ right = screenWidth; left = right - v.getWidth(); ranColor = 0xff000000 | random.nextInt(0x00ffffff); drawable.setColor(ranColor); } if(top < 0){ top = 0; bottom = top + v.getHeight(); ranColor = 0xff000000 | random.nextInt(0x00ffffff); drawable.setColor(ranColor); } if(bottom > screenHeight){ bottom = screenHeight; top = bottom - v.getHeight(); ranColor = 0xff000000 | random.nextInt(0x00ffffff); drawable.setColor(ranColor); } v.layout(left, top, right, bottom); Log.i("", "position:" + left +", " + top + ", " + right + ", " + bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return false; } }
下面是shape.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--填充色--> <solid android:color="#565745"/> <!-- 设置按钮的四个角为弧形 --> <!-- android:radius 弧形的半径 --> <corners android:radius="360dip"/> </shape>
源码下载 : https://github.com/duguodong258/duguodong#duguodong
相关文章推荐
- 如果写一个android桌面滑动切换屏幕的控件(二)
- RecyclerView实现滚动滑动以及点击改变控件颜色
- RecyclerView实现滚动滑动以及点击改变控件颜色
- Android-满屏幕拖动的控件
- Android在监听器中使用代码改变其他控件背景或颜色
- 控件:拖动条 --- SeekBar(改变屏幕亮度)
- 改变android控件颜色
- Android 改变switch控件颜色
- Android——滑动屏幕监听+ Palette获取图片中的颜色+Toolbar-ActionBar
- android 控件按下和放开后,字体颜色的改变
- Android-满屏幕拖动的控件
- Android-满屏幕拖动的控件
- Android-满屏幕拖动的控件
- Android 滑动改变标题/栏导航栏颜色、透明度
- android 三种方式改变控件文字的颜色
- Android 常见 View 控件 滑动 和 拖动的几种方式
- Android 标题栏和状态栏随ScrollView滑动颜色改变轻松实现
- Android GLSurfaceView学习(1):屏幕清理程序,使用随机颜色刷屏
- Android下ListView上下滑动背景颜色改变
- 运行时改变android控件颜色