• 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>
  • 相关阅读:
    C/C++程序内存的分配
    【解题报告】UVA101 The Blocks Problem
    洗牌算法
    【持续更新】算法竞赛常用模板
    北京邮电大学2021乐理公共选修课期末考试
    Oracle通用大数据量存储过程分页修正版
    (转)NHibernate 3.0在PetShop 3层架构中的应用
    Json.Net学习笔记
    JS获得浏览器高度和宽度参数
    jQuery数组处理汇总
  • 原文地址:https://www.cnblogs.com/zhengyanyan/p/5501886.html
Copyright © 2020-2023  润新知