網站首頁 編程語言 正文
本文實例為大家分享了Android自定義加載圈的具體代碼,供大家參考,具體內容如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context="com.tlkg.welcome.loadingviewdemo.MainActivity"> ? ? <com.tlkg.welcome.loadingviewdemo.LoadingView ? ? ? ? android:layout_width="150dp" ? ? ? ? android:layout_height="150dp" ? ? ? ? android:layout_centerInParent="true" /> </RelativeLayout>
public class LoadingView extends LinearLayout {
? ? public LoadingView(Context context) {
? ? ? ? this(context, null);
? ? }
? ? public LoadingView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);
? ? }
? ? public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? setOrientation(VERTICAL);
? ? ? ? setGravity(Gravity.CENTER);
? ? ? ? setBackgroundResource(R.drawable.loadingsp);
? ? ? ? LoadView loading = new LoadView(getContext());
? ? ? ? loading.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
? ? ? ? addView(loading);
? ? ? ? TextView tv = new TextView(getContext());
? ? ? ? tv.setText("正在加載中");
? ? ? ? LinearLayout.LayoutParams layoutParams = new LayoutParams(-2, -2);
? ? ? ? layoutParams.setMargins(0, 10, 0, 0);
? ? ? ? tv.setLayoutParams(layoutParams);
? ? ? ? tv.setGravity(Gravity.CENTER);
? ? ? ? tv.setTextColor(Color.WHITE);
? ? ? ? addView(tv);
? ? }
? ? class LoadView extends View {
? ? ? ? Paint mPaint;
? ? ? ? private int mWidth;
? ? ? ? private int mHeight;
? ? ? ? private int mCurrentIndex = 0;
? ? ? ? private int count = 12;
? ? ? ? public LoadView(Context context) {
? ? ? ? ? ? super(context);
? ? ? ? ? ? mPaint = new Paint();
? ? ? ? ? ? mPaint.setAntiAlias(true);
? ? ? ? ? ? mPaint.setStyle(Paint.Style.FILL);
? ? ? ? ? ? mPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()));
? ? ? ? ? ? mPaint.setColor(Color.WHITE);
? ? ? ? }
? ? ? ? @Override
? ? ? ? protected void onDraw(Canvas canvas) {
? ? ? ? ? ? super.onDraw(canvas);
? ? ? ? ? ? if (mCurrentIndex >= count) {
? ? ? ? ? ? ? ? mCurrentIndex = 0;
? ? ? ? ? ? }
? ? ? ? ? ? int endAlpha = 255 / count;
? ? ? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? ? ? int alpha;
? ? ? ? ? ? ? ? if (mCurrentIndex - i > 0) {
? ? ? ? ? ? ? ? ? ? alpha = endAlpha * (mCurrentIndex - i);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? alpha = 255 - 255 / count * (i - mCurrentIndex);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mPaint.setColor(Color.argb(alpha, 255, 255, 255));
? ? ? ? ? ? ? ? canvas.drawLine(mWidth / 2, 0, mWidth / 2, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()), mPaint);
? ? ? ? ? ? ? ? canvas.rotate(360 / count, mWidth / 2, mHeight / 2);
? ? ? ? ? ? }
? ? ? ? ? ? mCurrentIndex++;
? ? ? ? ? ? postInvalidateDelayed(100);
? ? ? ? }
? ? ? ? @Override
? ? ? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) {
? ? ? ? ? ? super.onSizeChanged(w, h, oldw, oldh);
? ? ? ? ? ? mWidth = getWidth();
? ? ? ? ? ? mHeight = getHeight();
? ? ? ? }
? ? }
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <corners android:radius="5dp" /> ? ? <solid android:color="#aa000000" /> </shape>
原文鏈接:https://blog.csdn.net/zhe_ge_sha_shou/article/details/75196308
相關推薦
- 2022-05-10 @requestMapping注解的常用參數
- 2023-07-27 express 請求方式(常用) / 不完整請求路徑
- 2023-02-07 C++11中的引用限定符示例代碼_C 語言
- 2023-11-19 如何卸載k3s?
- 2022-05-09 .Net?Core創建Api進行文件上傳功能_實用技巧
- 2022-12-03 GCC?指令詳解及動態庫、靜態庫的使用方法_其它綜合
- 2022-04-27 Python中python-nmap模塊的使用介紹_python
- 2022-05-18 python基礎教程之csv格式文件的寫入與讀取_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支