您好,登錄后才能下訂單哦!
本篇內容介紹了“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>
“Android如何自定義加載圈”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。