最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果

 更新時(shí)間:2017年12月12日 11:53:24   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一直想實(shí)現(xiàn)一個(gè)豎直跑馬燈的效果,今天接觸到了ViewFlipper這個(gè)控件, 是做安卓視圖切換的,  對(duì)其用自定義控件進(jìn)行包裝;實(shí)現(xiàn)其點(diǎn)擊回調(diào)和自定義視圖等功能

不多說(shuō),直接上代碼:

定義了一個(gè)自定義控件,  繼承LinearLayout

package com.example.viewfipperdemo; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.ViewFlipper; 
 
/** 
 * Created by zmybi on 2017/1/19. 
 */ 
 
public class MarqueeTextView extends LinearLayout { 
 
 private Context mContext; 
 private String[] strs; 
 private View mView; 
 
 private OnTextClickListener mOnTextClickListener; 
 private ViewFlipper mViewFlipper; 
 
 
 public MarqueeTextView(Context context) { 
  this(context,null); 
  this.mContext = context; 
 } 
 
 public MarqueeTextView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  this.mContext = context; 
 
  initBasicView(); 
 } 
 
 /** 
  * 用于外界向里面?zhèn)髦?并且初始化控件中的ViewFipper 
  * @param str 
  * @param onTextClickListener 
  */ 
 public void setData(String[] str,OnTextClickListener onTextClickListener) { 
  this.strs = str; 
  this.mOnTextClickListener = onTextClickListener; 
  initViewFipper(); 
 } 
 
 
 
 private void initBasicView() { 
  mView = LayoutInflater.from(mContext).inflate(R.layout.layout_viewfipper,null); 
  mViewFlipper = (ViewFlipper) mView.findViewById(R.id.viewflipper); 
 
  mViewFlipper.setInAnimation(mContext,R.anim.in); //進(jìn)來(lái)的動(dòng)畫 
  mViewFlipper.setOutAnimation(mContext,R.anim.out); //出去的動(dòng)畫 
 
  LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
  addView(mView,params); 
 
  mViewFlipper.startFlipping(); 
 } 
 
 /** 
  * 定義銷毀的方法 
  */ 
 public void clearViewFlipper() { 
  if(mView != null) { 
   if(mViewFlipper != null) { 
    mViewFlipper.stopFlipping(); 
    mViewFlipper.removeAllViews(); 
    mViewFlipper =null; 
   } 
   mView = null; 
  } 
 } 
 
 
 /** 
  * 初始化viewFipper中的自孩子視圖 
  */ 
 private void initViewFipper() { 
  if(strs.length == 0) { 
   return; 
  } 
 
  int i = 0; 
  mViewFlipper.removeAllViews(); 
  while (i < strs.length) { //循環(huán)3次 
   final TextView tv = new TextView(mContext); 
   tv.setText(strs[i]); 
 
   tv.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if(mOnTextClickListener != null) { 
      mOnTextClickListener.onClick(tv); 
     } 
    } 
   }); 
   mViewFlipper.addView(tv); 
   i++; 
  } 
 
 } 
} 

給viewFlipper設(shè)置動(dòng)畫的寫法:

in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
 
 <translate 
  android:duration="@android:integer/config_mediumAnimTime" 
  android:fromYDelta="50%p" 
  android:toYDelta="0" /> 
 <alpha 
  android:duration="@android:integer/config_mediumAnimTime" 
  android:fromAlpha="0.0" 
  android:toAlpha="1.0" /> 
 
</set> 

out.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
 
 <translate 
  android:duration="@android:integer/config_mediumAnimTime" 
  android:fromYDelta="0" 
  android:toYDelta="-50%p" /> 
 <alpha 
  android:duration="@android:integer/config_mediumAnimTime" 
  android:fromAlpha="1.0" 
  android:toAlpha="0.0" /> 
 
</set> 

我們看一下layout_viewflipper布局的寫法:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
 
 <ViewFlipper 
  android:padding="10dp" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:id="@+id/viewflipper" 
  android:background="#33ff0000" 
  android:flipInterval="2000" 
  ></ViewFlipper> 
 
</LinearLayout> 

其中flipInterval  是決定切換的時(shí)間的

我們?cè)賮?lái)看看MainActivity中的代碼:

package com.example.viewfipperdemo; 
 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class MainActivity extends AppCompatActivity { 
 
 /** 
  * 自定義的可滾動(dòng)的TextView 
  */ 
 private MarqueeTextView mMarqueeTextView; 
 
 private String[] str = {"我是1","我是2","我是3"}; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
 
  mMarqueeTextView = (MarqueeTextView) findViewById(R.id.marqueetextview); 
 
  mMarqueeTextView.setData(str, new OnTextClickListener() { 
   @Override 
   public void onClick(View view) { 
    Toast.makeText(MainActivity.this,((TextView)view).getText(),Toast.LENGTH_LONG).show(); 
   } 
  }); 
 } 
 
 @Override 
 protected void onDestroy() { 
  super.onDestroy(); 
  mMarqueeTextView.clearViewFlipper(); 
 } 
} 

對(duì)了,還定義了一個(gè)接口:

package com.example.viewfipperdemo; 
 
import android.view.View; 
 
/** 
 * Created by zmybi on 2017/1/19. 
 */ 
 
public interface OnTextClickListener { 
 
 void onClick(View view); 
} 

至此,如上的豎直跑馬燈就完成了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

新邵县| 正蓝旗| 特克斯县| 治多县| 上饶县| 南平市| 南宁市| 安岳县| 济源市| 昌宁县| 蒲江县| 仙桃市| 怀远县| 中阳县| 洛南县| 平南县| 图木舒克市| 体育| 精河县| 沧源| 蓬溪县| 崇义县| 手机| 城市| 祥云县| 昆山市| 安西县| 遂昌县| 观塘区| 凤山县| 荥阳市| 佳木斯市| 靖边县| 牙克石市| 长白| 东阳市| 印江| 嵩明县| 依安县| 柘城县| 贵州省|