Android 사용자 정의 TextBanner 자동 스크롤
7038 단어 AndroidTextBanner자동 스크롤
1、TextBanner
package com.example.myapplication.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;
public class TextBanner extends ViewGroup {
private List<String> mData = new ArrayList<>();
private ViewFlipper viewFlipper;
private int parentWidthSpec;
public TextBanner(Context context) {
super(context);
}
public TextBanner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextBanner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int top = 0;
int bottom = getChildAt(0).getMeasuredHeight();
int left = 0;
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
left = (parentWidthSpec - view.getMeasuredWidth()) / 2;
view.layout(left, top, left + view.getMeasuredWidth(), bottom);
top += view.getMeasuredHeight();
bottom = top + view.getMeasuredHeight();
}
Log.d("tzg", "bottom: " + bottom);
Log.d("tzg", "top: " + top);
}
public void setData(List<String> data) {
mData.clear();
if (data.isEmpty()) {
return;
}
this.mData = data;
setTextList();
}
private void setTextList() {
viewFlipper = (ViewFlipper) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_viewflip, this, false);
for (String mDatum : mData) {
TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_textview, this, false);
view.setText(mDatum);
viewFlipper.addView(view);
}
viewFlipper.setInAnimation(getContext(), R.anim.come_in);
viewFlipper.setOutAnimation(getContext(), R.anim.come_out);
viewFlipper.setFlipInterval(2000);
addView(viewFlipper);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
parentWidthSpec = MeasureSpec.getSize(widthMeasureSpec);
int parentHeightSpec = MeasureSpec.getSize(heightMeasureSpec);
int childWidth = MeasureSpec.makeMeasureSpec(parentWidthSpec, MeasureSpec.AT_MOST);
int childHeight = MeasureSpec.makeMeasureSpec(parentHeightSpec, MeasureSpec.AT_MOST);
int totalHeight = getChildAt(0).getMeasuredHeight();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
measureChild(view, childWidth, childHeight);
}
Log.d("tzg", "totalCount: " + totalHeight);
setMeasuredDimension(parentWidthSpec, totalHeight);
}
public void startAnimation() {
// 1、
// viewFlipper.startFlipping();
// 2、
viewFlipper.setAutoStart(true);
viewFlipper.isAutoStart();
}
}
사용 한 자원 1.애니메이션 자원
(1)、come_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0"/>
</set>
(2)、come_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%p"/>
</set>
2.레이아웃 자원(1)、flow_layout_viewflip.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</ViewFlipper>
(2)、flow_layout_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="demo"
android:textColor="#FF00FF" />
3.mainActivity 에서 의 사용
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.myapplication.customview.FlowLayout;
import com.example.myapplication.customview.TextBanner;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("111111111");
arrayList.add("222222222222444444444444");
arrayList.add(" 5");
arrayList.add(" 633");
arrayList.add(" a7 a7");
arrayList.add(" 7889");
arrayList.add(" 2323423423 ");
arrayList.add(" sdfsfada sdfsfada ");
arrayList.add(" 34345");
arrayList.add("pppppppp");
arrayList.add(" ");
arrayList.add(" ");
arrayList.add(" ");
arrayList.add(" ");
arrayList.add("woaoni");
arrayList.add(" ");
arrayList.add(" ");
TextBanner viewById = this.findViewById(R.id.text_banner);
viewById.setData(arrayList);
viewById.startAnimation();
}
}
구체 적 효과자체 측정 안 했 어 요. 버그 가 있 으 면 스스로 해결 해 요.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.