Androidっていわゆるスライドバーみたいな感じでビューの大きさを変えることって
標準のウィジェットでは用意されていない。
やからスライド(ドラッグ)で大きさを可変にできるビューを作ってみた。
なまえは適当にSlideBarDrugViewControllerとしてみた。
ソースはコチラ(Github)。以下は使い方の簡単なメモ。
レイアウト
スライドバー用のビューと、その上下にビューを用意する。
今回は例なのでビューの種類は何でもいい。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/view0"
android:gravity="center" />
<TextView
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SlideBar"
android:background="#6090ef"
android:gravity="center" />
<Button
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="BottomView" />
</LinearLayout>
MainActivity
Mainのアクティビティにレイアウトを読み込ませる。以下の2点を使用する。
- (1)で書かれている4つの引数を与える
- (2)のようにonTouchイベントをcontrollerに渡す
public class SlideBarWidgetActivity extends Activity {
Context context;
SlideBarDrugViewController controller;
View upperView;
View slidebarView;
View bottomView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
upperView = (View)this.findViewById(R.id.view0);
slidebarView = (View)this.findViewById(R.id.view1);
bottomView = (View)this.findViewById(R.id.view2);
context = this;
}
public void onWindowFocusChanged(boolean hasFocus) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final int screenHeight = display.getHeight();
Rect r = new Rect();
this.upperView.getGlobalVisibleRect(r);
if(controller == null){
/**
* (1)以下の4つの引数を与える
* drugView 可変ビュー(スライドバーの上部ビュー)
* slideBarView スライドバーとなるビュー
* minpx 可変ビューの上部(top)の絶対座標
* maxHeight スライドバーが下に動く最大サイズ。画面全体の大きさを入れると最下端まで移動させられる
*/
controller = new SlideBarDrugViewController(upperView, slidebarView, r.top, screenHeight);
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
// (2) onTouchイベントをcontrollerに委譲
return controller.onTouchEvent(event);
}
}
実行結果
画像では分かりにくいけど、ScrollBarをスライドさせるとそれにあわせて上下のビューの大きさが変化する。



0 件のコメント:
コメントを投稿