camera源码设置条,自己写了个类,具有其主要功能,同样有switcher改进

本文介绍了一种名为SetSeekBar的自定义Android控件,该控件可在不同活动间切换,并详细解释了其工作原理和使用方法。通过触摸事件,用户可以调整控件的位置并触发回调函数。

类代码:

/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.bar; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.ImageView; /** * A widget which switchs between the {@code Camera} and the {@code VideoCamera} * activities. */ public class SetSeekBar extends ImageView implements View.OnTouchListener { @SuppressWarnings("unused") private static final String TAG = "SetSeekBar"; /** A callback to be called when the user wants to switch activity. */ public interface OnSwitchListener { // Returns true if the listener agrees that the switch can be changed. public boolean onSwitchChanged(SetSeekBar source, int index); } private int mNumber = 0; private OnSwitchListener mListener; int drawableHeight = 0; int thumbHeight = 0; int positionIndex = 0; public SetSeekBar(Context context, AttributeSet attrs) { super(context, attrs); // Drawable drawable = getDrawable(); // drawableHeight = drawable.getIntrinsicHeight(); drawableHeight = 240; } //the thumb position number public void setNumber(int number){ mNumber = number; thumbHeight = drawableHeight/mNumber; Log.i(TAG,"thumbHeight = "+thumbHeight); } public int getNumber(){ return mNumber; } // Try to change the switch position. (The client can veto it.) //private void tryToSetSwitch(boolean onOff) public void setOnSwitchListener(OnSwitchListener listener) { mListener = listener; } @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) return false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.i("TAG","ACTION_DOWN"); // setPressed(true); setPosition(event); trackTouchEvent(event); break; case MotionEvent.ACTION_MOVE: trackTouchEvent(event); break; case MotionEvent.ACTION_UP: trackTouchEvent(event); // setPressed(false); break; case MotionEvent.ACTION_CANCEL: setPressed(false); break; } return true; } //the thumb position index private void setPosition(MotionEvent event){ int x = (int) event.getY(); positionIndex = getPosition(x); mListener.onSwitchChanged(this, positionIndex); Log.i("TAG","positionIndex = "+getPosition(x)); } private void trackTouchEvent(MotionEvent event) { int x = (int) event.getY(); if(positionIndex != getPosition(x)){ Log.i("TAG","positionIndex = "+getPosition(x)); positionIndex = getPosition(x); mListener.onSwitchChanged(this, positionIndex); } } private int getPosition(int x){ for(int i=1;i<=mNumber;i++){ if(x < thumbHeight*i) return i; } return mNumber; } // Consume the touch events for the specified view. public void addTouchView(View v) { v.setOnTouchListener(this); } // This implements View.OnTouchListener so we intercept the touch events // and pass them to ourselves. public boolean onTouch(View v, MotionEvent event) { onTouchEvent(event); return true; } }

使用:

<com.android.bar.SetSeekBar
android:id="@+id/bar"
android:layout_width="40dip"
android:layout_height="240dip"
android:background="@drawable/seek_bg"
android:layout_alignParentRight="true"
android:layout_marginRight="30dip"
/>

SetSeekBar indicatorBar = (SetSeekBar)findViewById(R.id.bar);
indicatorBar.setNumber(4);
indicatorBar.setOnSwitchListener(new OnSwitchListener(){
public boolean onSwitchChanged(SetSeekBar source, int index) {
//your code
return true;
}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值