2016년 5월 6일 금요일

안드로이드개발#Vertical SeekBar



안드로이드개발#Vertical SeekBar



안드로이드 개발에 있어서 SeekBar가 있어요
수평SeekBar가 있는데, 이를 커스터마이징해서 Vertical SeekBar를 만들수가 있어요

화면을 보세요~



우측에 보이는 것이 안드로이드에 여러가지 widget이 있는데, Vertical SeekBar가 되겠어요

범주를 100으로 둘때 현재 72정도라고 Toast msg로 보여주고 있어요

---------------------------------------------------------------

UI xml)

<LinearLayout 
         android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="bottom|right" 
     android:gravity="bottom|right" 
     android:paddingTop="30dip"  
     android:paddingBottom="30dip">
         <com.soft.light5.VerticalSeekBar  
             android:id="@+id/sbCapture" 
             android:layout_width="wrap_content"   
      android:layout_height="fill_parent" 
      android:max="100" 
      android:progress="0" >   
         </com.soft.light5.VerticalSeekBar> 
</LinearLayout>




java source)
VerticalSeekBar.java

package com.soft.light5;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    private OnSeekBarChangeListener onChangeListener;
    @Override
    public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
        this.onChangeListener = onChangeListener;
    }

    private int lastProgress = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            onChangeListener.onStartTrackingTouch(this);
            setPressed(true);
            setSelected(true);
            break;
        case MotionEvent.ACTION_MOVE:
            super.onTouchEvent(event);
            int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
             
            // Ensure progress stays within boundaries
            if(progress < 0) {progress = 0;}
            if(progress > getMax()) {progress = getMax();}
            setProgress(progress);  // Draw progress
            if(progress != lastProgress) {
                // Only enact listener if the progress has actually changed
                lastProgress = progress;
                onChangeListener.onProgressChanged(this, progress, true);
            }
             
            onSizeChanged(getWidth(), getHeight() , 0, 0);
            setPressed(true);
            setSelected(true);
            break;
        case MotionEvent.ACTION_UP:
            onChangeListener.onStopTrackingTouch(this);
            setPressed(false);
            setSelected(false);
            break;
        case MotionEvent.ACTION_CANCEL:
            super.onTouchEvent(event);
            setPressed(false);
            setSelected(false);
            break;
        }
        return true;
    }

    public synchronized void setProgressAndThumb(int progress) {
        setProgress(progress);
        onSizeChanged(getWidth(), getHeight() , 0, 0);
        if(progress != lastProgress) {
            // Only enact listener if the progress has actually changed
            lastProgress = progress;
            onChangeListener.onProgressChanged(this, progress, true);
        }
    }

    public synchronized void setMaximum(int maximum) {
        setMax(maximum);
    }

    public synchronized int getMaximum() {
        return getMax();
    }
}

-------------------------------------------------------------

사용 java)

VerticalSeekBar sbCapture = null;
...
sbCapture = (VerticalSeekBar)this.findViewById(R.id.sbCapture);
...
sbCapture.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), 
                    " progress value : " + iLcOnOffCnt, Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override 
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// value : progress
iLcOnOffCnt = progress;
}
});
...
sbCapture = null;
...

=====

괜찮은 요리레시피 안드로이드 어플을 소개해드려요~

방문해 주심에 감사드려요~
즐거운 시간 보내세요~

댓글 없음:

댓글 쓰기