Android轉盤按鈕效果巧妙實現

來源:互聯網
上載者:User

Android轉盤按鈕效果巧妙實現

想實現這樣一種效果:一個轉盤,旁邊有幾個按鈕分布。每個按鈕都可以點擊:

 


 

 

表面上看,有5個按鈕,其實其中每個按鈕都是一個大圓圈,放置的位置都是重疊的。只是按下去的那部分才會有顏色,其他都是透明。

 

 

看看這個布局檔案你就知道是怎麼擺放的:

 

其中自訂的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()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(); }}; }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.