//关于Seek的自定义样式,之前也有总结过,但是,一直做不出随着滑块移动的效果,查询了很多资料终于解决了这个问题,现在把代码写出来有bug的地方

希望大家批评指正。

Step 1 :自定义一个Viewgroup的类:

package com.example.seekbartest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup; public class TextMoveLayout extends ViewGroup { public TextMoveLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub } public TextMoveLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
} public TextMoveLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
} }

Step 2 : 在布局文件中放置简单的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.example.seekbartest.TextMoveLayout
android:id="@+id/textLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

Step 3 :在MainActivity中进行业务逻辑的操作

package com.example.seekbartest;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; @SuppressLint("NewApi") public class MainActivity extends ActionBarActivity {
private SeekBar sb;
private int step;
private TextMoveLayout textLayout;
private TextView text;
private int screenWidth;
private ViewGroup.LayoutParams params; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = (SeekBar)findViewById(R.id.seekBar1);
text = new TextView(this);
textLayout = (TextMoveLayout)findViewById(R.id.textLayout);
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
/* screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        text = new TextView(this);
        text.setBackgroundColor(Color.rgb(245, 245, 245));
        text.setTextColor(Color.rgb(0, 161, 229));
        text.setTextSize(16);
        layoutParams = new ViewGroup.LayoutParams(screenWidth, 50);
        textMoveLayout = (TextMoveLayout) findViewById(R.id.textLayout);
        textMoveLayout.addView(text, layoutParams);
        text.layout(0, 20, screenWidth, 80);*/
text.setBackgroundColor(Color.rgb(240, 240, 240));
text.setTextColor(Color.rgb(0, 161,229));
text.setTextSize(12);
params = new ViewGroup.LayoutParams(screenWidth, 50);
textLayout.addView(text, params);
text.layout(0, 20, screenWidth, 80); sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
hideText(); } @Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
showText(); } @Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
/* text.layout((int) (progress * moveStep), 20, screenWidth, 80);
text.setText(getCheckTimeBySeconds(progress, startTimeStr));*/
showText();
text.layout((int)progress*sb.getWidth()/sb.getMax(), 20, screenWidth, 80);
text.setText(progress+"%");
}
}); }
public void hideText(){
text.setAlpha(0); }
public void showText(){
text.setAlpha(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Step :效果图

不拖动滑块时的效果:

拖动滑块时的效果

可以随着SeekBar滑块滑动显示的Demo的更多相关文章

  1. tableviewcell滑动显示多个按钮UITableViewRowAction(转载)

    demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...

  2. duilib : 滑动显示的窗口实现以及 悬浮窗 (转载)

    1. vc 判断窗口是否显示  BOOL IsWindowVisible(HWND hWnd); 2.悬浮窗 http://blog.csdn.net/lincyang/article/details ...

  3. jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏

    1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...

  4. Android自定义滑动显示隐藏布局

    方式一:上下左右滑动显示隐藏布局 总结代码地址: http://git.oschina.net/anan9303/customView参考例子: http://www.jianshu.com/p/fc ...

  5. fullpage中大的图片超过一屏怎么在手机端滑动显示?

    fullpage中大的图片超过一屏怎么在手机端滑动显示?(设置overflow电脑端是会出现滚动条的,但是在手机端不出现滚动条,图片也不可左右滑动显示) var $window = $(window) ...

  6. JS实现可用滑块滑动的缓动图

    尝试模仿京东的"发现好货"模块的可用滑块滑动的缓动图 JS代码 function $(id) { return document.getElementById(id); } //缓 ...

  7. 【转】 为SeekBar滑块设置固定值以及自定义Seekbar,progressbar样式--不错

    原文网址:http://blog.csdn.net/jdsjlzx/article/details/7804080 最近在项目中使用到了seekbar和progressbar,且必须按照设计要求来进行 ...

  8. js做通讯录的索引滑动显示效果和滑动显示锚点效果

    只做实现..完全没考虑性能优化.所以我实现了以后特别卡. 第一个是在通讯录右边的索引条上进行滑动,滑动到相应字母就跳转到相应字母的锚点上. 思路:监听touchmove事件,获取clientX和cli ...

  9. QQ左侧滑动显示之按钮切换

    上一篇为大家介绍了关于自定义属性设置方法,本篇我将为大家介绍一下如何通过按钮来控制Menu的显示和隐藏,为了达到这个效果我们需要在SlidingMenu中添加三个方法,用来达到实现上述效果的目的. 我 ...

随机推荐

  1. SpringBoot非官方教程 | 第十九篇: 验证表单信息

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot19/ 本文出自方志朋的博客 这篇文篇主要简述如何 ...

  2. 在Win7虚拟机下搭建Hadoop2.6.0+Spark1.4.0单机环境

    Hadoop的安装和配置可以参考我之前的文章:在Win7虚拟机下搭建Hadoop2.6.0伪分布式环境. 本篇介绍如何在Hadoop2.6.0基础上搭建spark1.4.0单机环境. 1. 软件准备 ...

  3. linux下重新启动oracle

    第一步.以Oracle帐户进入Linux系统 第二步.执行以下命令查看数据库监听器的状况: lsnrctl status 或者查看数据库端口是否被监听(默认1521) netstat -ano | g ...

  4. 剑指offer js算法练习(1-10)

    1.二维数组中的查找       在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数, ...

  5. SQL:检索数据-基本检索

    检索数据 1.select语句 增删改查四大操作之"查",即检索: 一般包括:what,where:查什么,从哪里选择 2.检索单个列 例:想从products表中检索名为prod ...

  6. (转)IP地址分配原理

    网络模型介绍 在计算机网络中有著名的OSI七层协议体系结构,概念清楚,理论完整,但是它既复杂又不实用.TCP/IP体系结构则不同,得到的广泛的应用.最终结合OSI和TCP/IP的优点,采用了一种只有五 ...

  7. Java小功能大杂烩

    生成UUID: import java.util.UUID; public class ProductUUID { // 随机返回前十位的UUID public static String getUU ...

  8. Fibonacci使用递归和循环实现

    #include<stdio.h> double Fibonacci(int i); double Fibonacci_(int i); int main(void) { int i; p ...

  9. C# 隐藏窗口标题栏、隐藏任务栏图标

    //没有标题 this.FormBorderStyle = FormBorderStyle.None; //任务栏不显示 this.ShowInTaskbar = false;

  10. python入门——Anaconda安装

    初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...