欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)畫(huà) 
  mViewFlipper.setOutAnimation(mContext,R.anim.out); //出去的動(dòng)畫(huà) 
 
  LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
  addView(mView,params); 
 
  mViewFlipper.startFlipping(); 
 } 
 
 /** 
  * 定義銷(xiāo)毀的方法 
  */ 
 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)畫(huà)的寫(xiě)法:

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布局的寫(xiě)法:

<?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)文章

  • android控件實(shí)現(xiàn)單擊拖動(dòng)效果

    android控件實(shí)現(xiàn)單擊拖動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)單擊拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android下拉刷新以及GridView使用方法詳解

    Android下拉刷新以及GridView使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android下拉刷新以及GridView的使用方法,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android仿微信輸入框效果的實(shí)現(xiàn)代碼

    Android仿微信輸入框效果的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android仿微信輸入框效果的實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2017-05-05
  • Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-11-11
  • Kotlin中Stack與LinkedList的實(shí)現(xiàn)方法示例

    Kotlin中Stack與LinkedList的實(shí)現(xiàn)方法示例

    這篇文章主要給大家介紹了關(guān)于Kotlin中Stack與LinkedList實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)

    Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)

    這篇文章主要介紹了Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Android使用viewpager實(shí)現(xiàn)自動(dòng)無(wú)限輪播圖

    Android使用viewpager實(shí)現(xiàn)自動(dòng)無(wú)限輪播圖

    這篇文章主要介紹了Android使用viewpager實(shí)現(xiàn)自動(dòng)無(wú)限輪播圖效果,實(shí)現(xiàn)方法大概有兩種,一種是viewpager+作為游標(biāo)的點(diǎn) 。另外一種是重寫(xiě)viewpager,具體實(shí)現(xiàn)過(guò)程大家參考下本文
    2018-06-06
  • Android自定義圓點(diǎn)指示器

    Android自定義圓點(diǎn)指示器

    這篇文章主要為大家詳細(xì)介紹了Android自定義圓點(diǎn)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android中使用LayoutInflater要注意的一些坑

    Android中使用LayoutInflater要注意的一些坑

    LayoutInflater類(lèi)在我們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)用到,最近在使用中就遇到了一些問(wèn)題,所有下面這篇文章主要給大家總結(jié)了關(guān)于Android中使用LayoutInflater要注意的一些坑,希望通過(guò)這篇能讓大家避免走一些彎路,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看吧。
    2017-04-04
  • Android 使用Pull方法解析XML文件的方法

    Android 使用Pull方法解析XML文件的方法

    本篇文章是對(duì)Android中使用Pull方法解析XML文件的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論