Android自定義加載圈的方法
更新時間:2022年06月22日 08:27:23 作者:這個殺手不太累
這篇文章主要為大家詳細(xì)介紹了Android自定義加載圈的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android自定義加載圈的具體代碼,供大家參考,具體內(nèi)容如下
<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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)中l(wèi)ibs和jinLibs文件夾的作用詳解
這篇文章主要給大家介紹了關(guān)于Android開發(fā)中l(wèi)ibs和jinLibs文件夾的作用的相關(guān)資料,文中通過圖文及示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09android 中viewpager+fragment仿微信底部TAG完美漸變
這篇文章主要介紹了android 中viewpager+fragment仿微信底部TAG完美漸變,需要的朋友可以參考下2017-05-05Android Studio中CodeStyle模板的配置方式
這篇文章主要介紹了Android Studio中CodeStyle模板的配置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android?RecyclerLineChart實現(xiàn)圖表繪制教程
這篇文章主要為大家介紹了Android?RecyclerLineChart實現(xiàn)圖表繪制教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12