//关于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. wordpress二次开发第一个jquery对比

    $(document).ready(function(){ $("input").focus(function(){ $("input").css(" ...

  2. NDK下载地址

    官方下载地址 http://developer.android.com/ndk/downloads/index.html 没有提供旧版下载链接,只能修改链接方式下载 http://dl.google. ...

  3. c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...

  4. Java基础——数据结构总结

    目的 : 加强类与对象的内存分配理解,加强操作能力.理解数据结构. 结构 : 数据元素之间的关系. 数据结构 : 带有结构的数据对象. 线性结构: 各数据元素之间的逻辑以用一个线性序列简单的表达出现. ...

  5. sprinbboot 热部署 造成类加载器 不一致问题

    这里只说devtools的方式,注意以下的热部署方式在IDEA是默认没有打开自动编译的,手动编译需要快捷键(Ctrl+Shift+F9), 自动编译的修改配置如下:(注意刷新不要太快,会有1-2秒延迟 ...

  6. RMAN备份与恢复(三)--备份相关概念

    (1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件: --数据文件:备份指定的一个或多个数据文件: --表空间:备份指定的一个或多个表空间: --归档重 ...

  7. oracle net manager 数据传输安全

    oracle net manager来加密客户端与数据库之间或中间件与 数据库之间的网络传输数据 第一步:开始-->所有程序 -->oracle --> 配置和移植工具 --> ...

  8. 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释

    原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml       标题所代表的意思为 使用操作系统本地验证,一般不 ...

  9. noip模拟赛 动态仙人掌(并查集,贪心)

    思路: 贪心+并查集 因为45‘,所以可以很方便的算出每个仙人掌的最晚起跳(左端点) 右端点自然也能出来 先按左端点排序 如果他右面的和他相交,就更新 用并查集维护这个更新的关系 更新的同时维护高就好 ...

  10. java 读取图片并转化为二进制字符串

    本例子的目的在于测试往oracle数据库中插入blob字段 //以下代码源于:https://www.cnblogs.com/ywlx/p/4544179.html public static Str ...