高仿网易新闻顶部滑动条效果

这个是网易新闻的主界面,我们知道底部可以用tabhost实现,这个很容易,我们在其他软件中也会经常用到。

至于顶部的滑动条,个人感觉还是比较漂亮的所以今天也模仿了下,网易顶部滑动条的效果,由于初次模仿这种效果,可能有些地方还不够完美,不过基本已经实现,希望大家能够喜欢。

废话不多说,下面上代码:

首先是布局layout下的main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/layoutBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/big_button_up" android:orientation="horizontal" > <RelativeLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1.0" > <TextView android:id="@+id/tab1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="新闻" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1.0" > <TextView android:id="@+id/tab2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="体育" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1.0" > <TextView android:id="@+id/tab3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="娱乐" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1.0" > <TextView android:id="@+id/tab4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="更多" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/page" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/layoutBar" android:background="#ffffff" android:orientation="vertical" > </LinearLayout> </RelativeLayout>
下面是核心类,

package cn.com.karl.slider; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.TranslateAnimation; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; public class SliderBarActivity extends Activity { /** Called when the activity is first created. */ private RelativeLayout layout; private RelativeLayout layout1; private RelativeLayout layout2; private RelativeLayout layout3; private RelativeLayout layout4; private TextView tab1; private TextView tab2; private TextView tab3; private TextView tab4; private TextView first; private int current = 1; private LinearLayout page; private boolean isAdd = false; private int select_width; private int select_height; private int firstLeft; private int startLeft; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ layout = (RelativeLayout) findViewById(R.id.root); layout1 = (RelativeLayout) findViewById(R.id.layout1); layout2 = (RelativeLayout) findViewById(R.id.layout2); layout3 = (RelativeLayout) findViewById(R.id.layout3); layout4 = (RelativeLayout) findViewById(R.id.layout4); page=(LinearLayout) this.findViewById(R.id.page); tab1 = (TextView) findViewById(R.id.tab1); tab1.setOnClickListener(onClickListener); tab2 = (TextView) findViewById(R.id.tab2); tab2.setOnClickListener(onClickListener); tab3 = (TextView) findViewById(R.id.tab3); tab3.setOnClickListener(onClickListener); tab4 = (TextView) findViewById(R.id.tab4); tab4.setOnClickListener(onClickListener); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rl.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); first = new TextView(this); first.setTag("first"); first.setGravity(Gravity.CENTER); first.setBackgroundResource(R.drawable.slidebar); first.setText(tab1.getText()); View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page1, null); page.addView(view1); switch (current) { case 1: layout1.addView(first, rl); current = R.id.tab1; break; case 2: layout2.addView(first, rl); current = R.id.tab2; break; case 3: layout3.addView(first, rl); current = R.id.tab3; break; case 4: layout4.addView(first, rl); current = R.id.tab4; break; default: break; } } private void replace() { switch (current) { case R.id.tab1: changeTop(layout1); break; case R.id.tab2: changeTop(layout2); break; case R.id.tab3: changeTop(layout3); break; case R.id.tab4: changeTop(layout4); break; default: break; } } private void changeTop(RelativeLayout relativeLayout){ TextView old = (TextView) relativeLayout.findViewWithTag("first");; select_width = old.getWidth(); select_height = old.getHeight(); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(select_width, select_height); rl.leftMargin = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft(); rl.topMargin = old.getTop() + ((RelativeLayout)old.getParent()).getTop(); firstLeft = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft(); TextView tv = new TextView(this); tv.setTag("move"); tv.setBackgroundResource(R.drawable.slidebar); layout.addView(tv , rl); relativeLayout.removeView(old); } private OnClickListener onClickListener = new OnClickListener(){ public void onClick(View v) { if(!isAdd){ replace(); isAdd = true; } TextView top_select = (TextView) layout.findViewWithTag("move"); top_select.setGravity(Gravity.CENTER); top_select.setText(tab1.getText()); int tabLeft; int endLeft = 0; boolean run = false; switch (v.getId()) { case R.id.tab1: if (current != R.id.tab1) { page.removeAllViews(); tabLeft = ((RelativeLayout) tab1.getParent()).getLeft() + tab1.getLeft() + tab1.getWidth() / 2; endLeft = tabLeft - select_width / 2; current = R.id.tab1; top_select.setText(tab1.getText()); run = true; View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page1, null); page.addView(view1); } break; case R.id.tab2: if (current != R.id.tab2) { page.removeAllViews(); tabLeft = ((RelativeLayout) tab2.getParent()).getLeft() + tab2.getLeft() + tab2.getWidth() / 2; endLeft = tabLeft - select_width / 2; current = R.id.tab2; top_select.setText(tab2.getText()); run = true; View view2=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page2, null); page.addView(view2); } break; case R.id.tab3: if (current != R.id.tab3) { page.removeAllViews(); tabLeft = ((RelativeLayout) tab3.getParent()).getLeft() + tab3.getLeft() + tab3.getWidth() / 2; endLeft = tabLeft - select_width/2; current = R.id.tab3; top_select.setText(tab3.getText()); run = true; View view3=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page3, null); page.addView(view3); } break; case R.id.tab4: if (current != R.id.tab4) { page.removeAllViews(); tabLeft = ((RelativeLayout) tab4.getParent()).getLeft() + tab3.getLeft() + tab4.getWidth() / 2; endLeft = tabLeft - select_width/2; current = R.id.tab4; top_select.setText(tab4.getText()); run = true; View view4=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page4, null); page.addView(view4); } break; default: break; } if(run){ TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - firstLeft, 0, 0); startLeft = endLeft - firstLeft; animation.setDuration(100); animation.setFillAfter(true); top_select.bringToFront(); top_select.startAnimation(animation); } } }; }

由于时间比较紧,我没有做注释,有时间再做注释啊。

看一下效果是不是一样啊!



效果还请大家自行体验并改进,由于时间仓促,代码并未做注释,希望大家能够原谅,下面我附上源码下载地址:点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现高仿微信语音播放效果,可以使用HTML5的audio标签。 首先,你需要准备好两个不同状态下的语音图标,一个表示未播放状态,一个表示播放状态。可以使用CSS设置这些图标,并使用JavaScript来切换它们的显示。 然后,在HTML中,你需要为每个语音添加一个audio元素,设置它的src属性为对应的音频文件路径,以及其他属性如controls、preload等。 接下来,你需要为每个语音添加一个点击事件监听器,在点击时播放对应的音频。你可以使用audio元素的play()方法来播放音频,同时切换语音图标状态。 最后,如果需要,你可以使用JavaScript监听audio元素的ended事件,在音频播放完毕后自动切换回未播放状态的语音图标。 下面是一个简单的示例代码: HTML部分: ```html <div class="voice"> <img src="voice-icon.png" class="voice-icon" data-src="audio.mp3"> <audio src="audio.mp3"></audio> </div> ``` CSS部分: ```css .voice-icon { width: 20px; height: 20px; background-image: url('voice-icon.png'); background-size: cover; } .voice-icon.playing { background-image: url('voice-icon-playing.png'); } ``` JavaScript部分: ```javascript document.querySelectorAll('.voice').forEach(function(voice) { var audio = voice.querySelector('audio'); var icon = voice.querySelector('.voice-icon'); voice.addEventListener('click', function() { if (audio.paused) { audio.play(); icon.classList.add('playing'); } else { audio.pause(); icon.classList.remove('playing'); } }); audio.addEventListener('ended', function() { icon.classList.remove('playing'); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值