zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Javascript的私有变量和方法、共有变量和方法以及特权方法、构造器、静态共有属性和静态共有方法
    Spring4整合Hibernate4出现的错误的解决
    Javascript类的创建
    Kettle学习总结(一)
    Kettle Excel导入数据到数据库
    Python 爬虫 2 (转)
    Js仿腾讯微博效果
    飘雪效果
    列表获取对应图片
    飞入购物车
  • 原文地址:https://www.cnblogs.com/zhengyanyan/p/5501886.html
Copyright © 2011-2022 走看看