Android转盘按钮效果巧妙实现
来源:互联网 发布:怎么更换路由器mac地址 编辑:程序博客网 时间:2024/06/11 14:48
想实现这样一种效果:一个转盘,旁边有几个按钮分布。每个按钮都可以点击:
表面上看,有5个按钮,其实其中每个按钮都是一个大圆圈,放置的位置都是重叠的。只是按下去的那部分才会有颜色,其他都是透明。
看看这个布局文件你就知道是怎么摆放的:
<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" android:background="@drawable/back_ground_yellow" tools:context="com.example.circle.MainActivity$PlaceholderFragment" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/picture" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/white_picture" /> <com.example.circle.TrapezoidImageButton android:id="@+id/telId1" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/select_menu_1" android:tag="telId1" /> <com.example.circle.TrapezoidImageButton android:id="@+id/telId2" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/select_menu_2" android:tag="telId12" /> <com.example.circle.TrapezoidImageButton android:id="@+id/telId3" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/select_menu_3" android:tag="telId3" /> <com.example.circle.TrapezoidImageButton android:id="@+id/telId4" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/select_menu_4" android:tag="telId4" /> <com.example.circle.TrapezoidImageButton android:id="@+id/telId5" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/select_menu_5" android:tag="telId5" /></RelativeLayout>
其中自定义的ImageButton
public class TrapezoidImageButton extends ImageButton { public TrapezoidImageButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public TrapezoidImageButton(Context context, AttributeSet attrs) { super(context, attrs); } public TrapezoidImageButton(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { if (isTouchPointInView(event.getX(),event.getY())|| event.getAction() != MotionEvent.ACTION_DOWN){ return super.onTouchEvent(event); }else{ return false; } } protected boolean isTouchPointInView(float localX, float localY){ Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); draw(canvas); int x = (int)localX; int y = (int)localY; if (x < 0 || x >= getWidth()) return false; if (y < 0 || y >= getHeight()) return false; int pixel = bitmap.getPixel(x,y); if ((pixel&0xff000000) != 0){ //不是透明的,这里是最关键所在。 return true; }else{ return false; } }}
主Activity:
package com.example.circle;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState == null) {getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}/** * A placeholder fragment containing a simple view. */public static class PlaceholderFragment extends Fragment {private Activity mactivity;public PlaceholderFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container,false);mactivity = getActivity();rootView.findViewById(R.id.telId1).setOnClickListener(onclick);rootView.findViewById(R.id.telId2).setOnClickListener(onclick);rootView.findViewById(R.id.telId3).setOnClickListener(onclick);rootView.findViewById(R.id.telId4).setOnClickListener(onclick);rootView.findViewById(R.id.telId5).setOnClickListener(onclick);return rootView;}public OnClickListener onclick = new OnClickListener() { @Overridepublic void onClick(View v) { Toast.makeText(mactivity, v.getTag().toString(), 1*1000).show(); }}; }}
在这里也分享一个demo源代码下载地址点击打开链接
1 0
- Android转盘按钮效果巧妙实现
- Android实现转盘效果
- android实现大转盘效果
- Android记录9--实现转盘效果
- 利用Android自定义View实现转盘旋转的效果
- Android自定义View实现转盘旋转的效果
- 利用surfaceview实现转盘效果
- Android实现抽奖转盘
- android 自定义多功能按钮(转盘样式)
- 自定义View实现转盘旋转效果
- cocos2dx实现转盘旋转外加粒子效果
- 3种转盘抽奖动画效果实现
- Android转盘菜单的实现
- Android 抽奖转盘的实现
- Android简单转盘的实现
- Android实现抽奖转盘动画
- Android抽奖转盘的实现
- android一个转盘效果的容器viewgroup
- 第一章 知识结构导图
- lease paper
- C# 利用反射访问修改私有成员
- Java SkipList 实现
- spring 事务传播行为和事务隔离级别
- Android转盘按钮效果巧妙实现
- 关于android工程目录下面android private libraries 和android dependences 以及reference libraries之间的种种
- maven学习教程(二)—— 常见命令、配置解析、HelloWorld
- maven项目使用Teamcity发布时出现400错误的解决方案
- 关于MFT的消息处理
- 《数据结构(C++)版》之第4章字符串和数组
- 求二维数组中的鞍点【数组】
- ios8 Autolayout实现uitableviewcell 高度自适应
- javaweb-day01-5(XML 解析-概述)