Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果
一直想實(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í)有所幫助,也希望大家多多支持腳本之家。
- android自定義控件ImageView實(shí)現(xiàn)圓形圖片
- Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
- Android自定義控件打造絢麗平行空間引導(dǎo)頁(yè)
- Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能
- Android自定義控件EditText使用詳解
- Android自定義控件實(shí)現(xiàn)下拉刷新效果
- 基于Android自定義控件實(shí)現(xiàn)雷達(dá)效果
- Android編程實(shí)現(xiàn)自定義控件的方法示例
- Android自定義控件之日期選擇控件使用詳解
- Android自定義控件實(shí)現(xiàn)九宮格解鎖功能
- 實(shí)例講解Android自定義控件
相關(guān)文章
android控件實(shí)現(xiàn)單擊拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)單擊拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android仿微信輸入框效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿微信輸入框效果的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-05-05Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11Kotlin中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-06Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)
這篇文章主要介紹了Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Android使用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-06Android中使用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