textview跟随seekbar的拖动改变位置

Posted zyy明天你好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

以上是关于textview跟随seekbar的拖动改变位置的主要内容,如果未能解决你的问题,请参考以下文章

拖动条SeekBar

SeekBar拖动条的功能和用法

Android中使用SeekBar拖动条实现改变图片透明度

android自定义进度值可拖动的seekbar

android自定义进度值可拖动的seekbar

Android SeekBar,动态显示进度且随SeekBar一起移动