這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么通過自定義ViewFipper控件實(shí)現(xiàn)豎直跑馬燈效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的泊頭網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
定義了一個(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;
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)來的動(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í)間的
我們再來看看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();
}
}對了,還定義了一個(gè)接口:
package com.example.viewfipperdemo;
import android.view.View;
public interface OnTextClickListener {
void onClick(View view);
}關(guān)于Android中怎么通過自定義ViewFipper控件實(shí)現(xiàn)豎直跑馬燈效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文標(biāo)題:Android中怎么通過自定義ViewFipper控件實(shí)現(xiàn)豎直跑馬燈效果
鏈接分享:http://chinadenli.net/article0/jgpooo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站改版、網(wǎng)頁設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)