Android turntable buttons are cleverly implemented
To achieve this effect, there are several buttons next to a turntable. You can click each button:
On the surface, there are five buttons. In fact, each of them is a big circle, and the positions are overlapped. Only the part that follows will be colored, and the rest will be transparent.
Look at this layout file and you will know how to place it:
The custom ImageButton
Public class extends ImageButton {public TrapezoidImageButton (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public aggregate (Context context, AttributeSet attrs) {super (context, attrs);} public TrapezoidImageButton (Context context) {super (context) ;}@ Override public boolean onTouchEvent (MotionEvent event) {if (isTouchPo) IntInView (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 () r Eturn false; int pixel = bitmap. getPixel (x, y); if (pixel & 0xff000000 )! = 0) {// not transparent. Here is the key. Return true;} else {return false ;}}}
Main 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()www.bkjia.com.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(); }}; }}