Android中怎么實現京東快報無限輪播效果?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
public class NoticeView extends ViewFlipper implements View.OnClickListener {
private Context mContext;
private List<String> mNotices;
private OnNoticeClickListener mOnNoticeClickListener;
public NoticeView(Context context) {
super(context);
}
public NoticeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mContext = context;
// 輪播間隔時間為3s
setFlipInterval(3000);
// 內邊距5dp
setPadding(dp2px(5f), dp2px(5f), dp2px(5f), dp2px(5f));
// 設置enter和leave動畫
setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notice_in));
setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notice_out));
}
/**
* 添加需要輪播展示的公告
*
* @param notices
*/
public void addNotice(List<String> notices) {
mNotices = notices;
removeAllViews();
for (int i = 0; i < mNotices.size(); i++) {
// 根據公告內容構建一個TextView
String notice = notices.get(i);
TextView textView = new TextView(mContext);
textView.setSingleLine();
textView.setText(notice);
textView.setTextSize(20f);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTextColor(Color.parseColor("#666666"));
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
// 將公告的位置設置為textView的tag方便點擊是回調給用戶
textView.setTag(i);
textView.setOnClickListener(this);
// 添加到ViewFlipper
NoticeView.this.addView(textView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
String notice = (String) mNotices.get(position);
if (mOnNoticeClickListener != null) {
mOnNoticeClickListener.onNotieClick(position, notice);
}
}
/**
* 通知點擊監聽接口
*/
public interface OnNoticeClickListener {
void onNotieClick(int position, String notice);
}
/**
* 設置通知點擊監聽器
*
* @param onNoticeClickListener 通知點擊監聽器
*/
public void setOnNoticeClickListener(OnNoticeClickListener onNoticeClickListener) {
mOnNoticeClickListener = onNoticeClickListener;
}
private int dp2px(float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue,
mContext.getResources().getDisplayMetrics());
}
}布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20sp" android:layout_marginLeft="15sp" android:layout_marginRight="15sp" android:layout_marginTop="20sp" android:background="@drawable/jingdong_news_bgcolor" android:orientation="horizontal" android:paddingLeft="15sp" android:paddingRight="15sp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/jd_news_tit" /> <com.project.jingdong.customview.NoticeView android:id="@+id/notice_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_horizontal|center_vertical" android:layout_weight="1"></com.project.jingdong.customview.NoticeView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:text=" | 更多 " android:textSize="22sp" /> </LinearLayout>
布局的樣式
<?xml version="1.0" encoding="utf-8"?><!-- 定義圓角矩形 --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:shape="rectangle"> <!-- 填充顏色 --> <solid android:color="#FFFFFF" /> <!-- 圓角 --> <corners android:bottomLeftRadius="16dp" android:bottomRightRadius="16dp" android:topLeftRadius="16dp" android:topRightRadius="16dp" /> <!-- 邊框顏色 --> <stroke android:width="1dip" android:color="#FFFFFF" /> </shape>
公告內容進入動畫notice_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>
公告內容滑出動畫notice_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>
在Activity或者Fragment中直接使用就可以了
//定義成為一個方法,直接調用就行了
private void init() {
NoticeView noticeView = (NoticeView) getActivity().findViewById(R.id.notice_view);
List<String> notices = new ArrayList<>();
notices.add("大促銷下單拆福袋,億萬新年紅包隨便拿");
notices.add("家電五折團,搶十億無門檻現金紅包");
notices.add("星球大戰剃須刀首發送200元代金券");
noticeView.addNotice(notices);
noticeView.startFlipping();
}關于Android中怎么實現京東快報無限輪播效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。