定制View中实现轨迹球功能示例

[size=medium][color=red]main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="20dip">
<RelativeLayout android:id="@+id/up"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:gravity="center">
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_1" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_2" android:layout_alignParentTop="true"
android:layout_marginLeft="10dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/toggle_button_1" />
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_3" android:layout_alignParentTop="true"
android:layout_marginLeft="10dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/toggle_button_2" />
</RelativeLayout>

<RelativeLayout android:id="@+id/down"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dip" android:gravity="center"
android:layout_below="@+id/up">
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_4" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_5" android:layout_alignParentTop="true"
android:layout_marginLeft="10dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/toggle_button_4" />
<com.android.customview.CustomToggleButton
android:id="@+id/toggle_button_6" android:layout_alignParentTop="true"
android:layout_marginLeft="10dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/toggle_button_5" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

custom_toggle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dip" android:layout_marginTop="0dip"
android:layout_height="46dip" android:background="@+drawable/button_bg_normal"
android:id="@+id/custom_toggle_button" android:orientation="vertical" >
<TextView android:id="@+id/toggle_button_text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginTop="50dip"
android:textColor="@+color/red" android:layout_marginLeft="20dip" />
<ImageView android:id="@+id/toggle_button_lightbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/toggle_button_text"
android:layout_marginTop="10dip" android:src="@+drawable/llightbar_off"
android:layout_marginLeft="5dip" />
</LinearLayout>


CustomView.java
package com.android.customview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class CustomView extends Activity implements OnClickListener {

private CustomToggleButton mToggleButton1;
private CustomToggleButton mToggleButton2;
private CustomToggleButton mToggleButton3;
private CustomToggleButton mToggleButton4;
private CustomToggleButton mToggleButton5;
private CustomToggleButton mToggleButton6;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToggleButton1 = (CustomToggleButton)findViewById(R.id.toggle_button_1);
mToggleButton1.setText("Button1");
mToggleButton1.setOnClickListener(this);
mToggleButton2 = (CustomToggleButton)findViewById(R.id.toggle_button_2);
mToggleButton2.setText("Button2");
mToggleButton2.setOnClickListener(this);
mToggleButton3 = (CustomToggleButton)findViewById(R.id.toggle_button_3);
mToggleButton3.setText("Button3");
mToggleButton3.setOnClickListener(this);
mToggleButton4 = (CustomToggleButton)findViewById(R.id.toggle_button_4);
mToggleButton4.setText("Button4");
mToggleButton4.setOnClickListener(this);
mToggleButton5 = (CustomToggleButton)findViewById(R.id.toggle_button_5);
mToggleButton5.setText("Button5");
mToggleButton5.setOnClickListener(this);
mToggleButton6 = (CustomToggleButton)findViewById(R.id.toggle_button_6);
mToggleButton6.setText("Button6");
mToggleButton6.setOnClickListener(this);

addTrackballItems();
}

@Override
public void onClick(View v) {
int resId = v.getId();
handleButtonEvent(resId);
}

private void handleButtonEvent(int resId){
switch (resId){
case R.id.toggle_button_1:
mToggleButton1.setChecked(true);
Intent intent = new Intent();
intent.setClass(this, TestBringToFront.class);
startActivity(intent);
break;
case R.id.toggle_button_2:
mToggleButton2.setChecked(true);
break;
case R.id.toggle_button_3:
mToggleButton3.setChecked(true);
break;
case R.id.toggle_button_4:
mToggleButton4.setChecked(true);
break;
case R.id.toggle_button_5:
mToggleButton5.setChecked(true);
break;
case R.id.toggle_button_6:
mToggleButton6.setChecked(true);
break;
}
}

private int convertIndexToResId(int curIndex){
if (curIndex == 0){
return R.id.toggle_button_1;
}else if (curIndex == 1){
return R.id.toggle_button_2;
}else if (curIndex == 2){
return R.id.toggle_button_3;
}else if (curIndex == 3){
return R.id.toggle_button_4;
}else if (curIndex == 4){
return R.id.toggle_button_5;
}else {
return R.id.toggle_button_6;
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_UP:
searchUpFocus();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
searchDownFocus();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
searchRightFocus();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
searchLeftFocus();
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
handleButtonEvent(convertIndexToResId(mCurTrackballIndex));
break;
}
return super.onKeyDown(keyCode, event);
}

private ArrayList<View> mTrackballItems = new ArrayList<View>();
private int mCurTrackballIndex = 0;
private int mTrackballItemsCount = 0;
private void addTrackballItems(){
mTrackballItems.add(mToggleButton1); // index = 0
mTrackballItems.add(mToggleButton2); // index = 1
mTrackballItems.add(mToggleButton3); // index = 2
mTrackballItems.add(mToggleButton4); // index = 3
mTrackballItems.add(mToggleButton5); // index = 4
mTrackballItems.add(mToggleButton6); // index = 5
mTrackballItemsCount = mTrackballItems.size();
}

private void searchUpFocus(){
if (mTrackballItems.get(mCurTrackballIndex).isSelected()){
if (mCurTrackballIndex - 3 >= 0){
mTrackballItems.get(mCurTrackballIndex).setSelected(false);
mCurTrackballIndex = mCurTrackballIndex - 3;
}else {
return;
}
}else {
mCurTrackballIndex = 0;
}
mTrackballItems.get(mCurTrackballIndex).setSelected(true);
setCurFocus(mCurTrackballIndex);
}

private void searchDownFocus(){
if (mTrackballItems.get(mCurTrackballIndex).isSelected()){
if (mCurTrackballIndex - 3 < 0){
mTrackballItems.get(mCurTrackballIndex).setSelected(false);
mCurTrackballIndex = mCurTrackballIndex + 3;
}else {
return;
}
}else {
mCurTrackballIndex = 0;
}
mTrackballItems.get(mCurTrackballIndex).setSelected(true);
setCurFocus(mCurTrackballIndex);
}

private void searchRightFocus(){
boolean isVaildUpRow = (mCurTrackballIndex >= 0) && (mCurTrackballIndex < 3);
boolean isVaildDownRow = (mCurTrackballIndex >= 3) && (mCurTrackballIndex < 5);

if (mTrackballItems.get(mCurTrackballIndex).isSelected()){
if (isVaildUpRow || isVaildDownRow){
mTrackballItems.get(mCurTrackballIndex).setSelected(false);
mCurTrackballIndex = mCurTrackballIndex + 1;
}else {
return;
}
}else {
mCurTrackballIndex = 0;
}
mTrackballItems.get(mCurTrackballIndex).setSelected(true);
setCurFocus(mCurTrackballIndex);
}

private void searchLeftFocus(){
boolean isVaildUpRow = (mCurTrackballIndex > 0) && (mCurTrackballIndex <= 3);
boolean isVaildDownRow = (mCurTrackballIndex > 3) && (mCurTrackballIndex <= 5);

if (mTrackballItems.get(mCurTrackballIndex).isSelected()){
if (isVaildUpRow || isVaildDownRow){
mTrackballItems.get(mCurTrackballIndex).setSelected(false);
mCurTrackballIndex = mCurTrackballIndex - 1;
}else {
return;
}
}else {
mCurTrackballIndex = 0;
}
mTrackballItems.get(mCurTrackballIndex).setSelected(true);
setCurFocus(mCurTrackballIndex);
}

private void setCurFocus(int curIndex){
clearAllFocus();
((CustomToggleButton)mTrackballItems.get(curIndex)).setBgFocus();
}

private void clearAllFocus(){
for (int i = 0; i < mTrackballItemsCount; i++){
((CustomToggleButton)mTrackballItems.get(i)).clearBgFocus();
}
}
}

CustomToggleButton.java
package com.android.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomToggleButton extends LinearLayout{

private Context mContext;
private LinearLayout mCustomToggleButton;
private TextView mText;
private ImageView mLightbar;

public CustomToggleButton(Context context) {
super(context);
mContext = context;
initView();
}

public CustomToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initView();
}

private void initView(){
LinearLayout l = (LinearLayout)View.inflate(mContext, R.layout.custom_toggle_button, null);
addView(l);
mCustomToggleButton = (LinearLayout)findViewById(R.id.custom_toggle_button);
// mCustomToggleButton.setOnClickListener(this);
mText = (TextView)findViewById(R.id.toggle_button_text);
mLightbar = (ImageView)findViewById(R.id.toggle_button_lightbar);
}

public void setText(String text){
mText.setText(text);
}

public void setChecked(boolean isChecked){
if (isChecked){
mLightbar.setImageResource(R.drawable.lightbar_on);
}else {
mLightbar.setImageResource(R.drawable.lightbar_off);
}
}

public void setBgFocus(){
mCustomToggleButton.setBackgroundResource(R.drawable.button_bg_focus);
}

public void clearBgFocus(){
mCustomToggleButton.setBackgroundResource(R.drawable.button_bg_normal);
}

}[/color][/size]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值