Android自定義控件ViewFipper實現(xiàn)豎直跑馬燈效果
一直想實現(xiàn)一個豎直跑馬燈的效果,今天接觸到了ViewFlipper這個控件, 是做安卓視圖切換的, 對其用自定義控件進(jìn)行包裝;實現(xiàn)其點擊回調(diào)和自定義視圖等功能
不多說,直接上代碼:
定義了一個自定義控件, 繼承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)來的動畫 mViewFlipper.setOutAnimation(mContext,R.anim.out); //出去的動畫 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è)置動畫的寫法:
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 是決定切換的時間的
我們再來看看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 { /** * 自定義的可滾動的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(); } }
對了,還定義了一個接口:
package com.example.viewfipperdemo; import android.view.View; /** * Created by zmybi on 2017/1/19. */ public interface OnTextClickListener { void onClick(View view); }
至此,如上的豎直跑馬燈就完成了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義控件ImageView實現(xiàn)圓形圖片
- Android自定義控件ImageView實現(xiàn)點擊之后出現(xiàn)陰影效果
- Android自定義控件打造絢麗平行空間引導(dǎo)頁
- Android自定義控件EditText實現(xiàn)清除和抖動功能
- Android自定義控件EditText使用詳解
- Android自定義控件實現(xiàn)下拉刷新效果
- 基于Android自定義控件實現(xiàn)雷達(dá)效果
- Android編程實現(xiàn)自定義控件的方法示例
- Android自定義控件之日期選擇控件使用詳解
- Android自定義控件實現(xiàn)九宮格解鎖功能
- 實例講解Android自定義控件
相關(guān)文章
Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例,非常具有實用價值,需要的朋友可以參考下2017-11-11Kotlin中Stack與LinkedList的實現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于Kotlin中Stack與LinkedList實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Android實現(xiàn)一個比相冊更高大上的左右滑動特效(附源碼)
這篇文章主要介紹了Android實現(xiàn)一個比相冊更高大上的左右滑動特效(附源碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Android使用viewpager實現(xiàn)自動無限輪播圖
這篇文章主要介紹了Android使用viewpager實現(xiàn)自動無限輪播圖效果,實現(xiàn)方法大概有兩種,一種是viewpager+作為游標(biāo)的點 。另外一種是重寫viewpager,具體實現(xiàn)過程大家參考下本文2018-06-06Android中使用LayoutInflater要注意的一些坑
LayoutInflater類在我們?nèi)粘i_發(fā)中經(jīng)常會用到,最近在使用中就遇到了一些問題,所有下面這篇文章主要給大家總結(jié)了關(guān)于Android中使用LayoutInflater要注意的一些坑,希望通過這篇能讓大家避免走一些彎路,需要的朋友可以參考學(xué)習(xí),下面來一起看吧。2017-04-04