textview跟随seekbar的拖动改变位置

代码里面;

package com.example.seek;



import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {
    private TextView textView;
    private SeekBar seekBar;
    private double width, fDensity;
    private int numbers = 0;
    
    private DisplayMetrics displaysMetrics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
         seekBar=(SeekBar) findViewById(R.id.seekBar1);
         textView=(TextView) findViewById(R.id.textView1);
        
        initView();
        
    }
    // 获取屏幕信息,以及初始化操作
    private void initView() {
        displaysMetrics = getResources().getDisplayMetrics();
        width = displaysMetrics.widthPixels;
        fDensity = (width - dip2px(this, 51)) / 100;
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        textView = (TextView) findViewById(R.id.textView1);
        initSeekBarProgress();
    }

    // TextView跟随SeekBar移动
    private void initSeekBarProgress() {
        seekBar.setProgress(numbers);
        seekBar.setOnSeekBarChangeListener(mSeekChange);
        LayoutParams paramsStrength = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        paramsStrength.leftMargin = (int) (numbers * fDensity);
        textView.setLayoutParams(paramsStrength);
        textView.setText(numbers + "");
    }

    private OnSeekBarChangeListener mSeekChange = new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            numbers = progress;
            LayoutParams paramsStrength = new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            paramsStrength.leftMargin = (int) (progress * fDensity);
            textView.setLayoutParams(paramsStrength);
            textView.setText(numbers + "");

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    };
    /**
     * 根据手机分辨率从 px(像素) 单位 转成 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机分辨率从 dp 单位 转成 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    

}

xml里面;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="83dp"
        android:text="TextView" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1" />

</RelativeLayout>

 

posted @ 2016-05-17 15:37  zyy明天你好  阅读(903)  评论(0编辑  收藏  举报