import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
public class ArcConfiguration {
private SimpleArcLoader.STYLE mLoaderStyle;
private Typeface mTypeFace = null;
private String mText = "Loading..";
private int mTextSize = 0;
private int mTextColor = Color.WHITE;
private int mArcMargin;
private int mAnimationSpeed;
private int mStrokeWidth;
private boolean mArcCircle;
private int mColors[] = {Color.parseColor("#F90101"), // RED
Color.parseColor("#0266C8"), // BLUE
Color.parseColor("#F2B50F"), // YELLOW
Color.parseColor("#00933B")};// GREEN
// =============================================================================================
// CONSTRUCTOR
// =============================================================================================
private ArcConfiguration() {}
public ArcConfiguration(Context context) {
// Default Values
mLoaderStyle = SimpleArcLoader.STYLE.SIMPLE_ARC;
mArcMargin = SimpleArcLoader.MARGIN_MEDIUM;
mAnimationSpeed = SimpleArcLoader.SPEED_MEDIUM;
mStrokeWidth = (int) context.getResources().getDimension(R.dimen.stroke_width);
}
public ArcConfiguration(Context context, SimpleArcLoader.STYLE mArcStyle) {
this(context);
mLoaderStyle = mArcStyle;
}
// =============================================================================================
// METHODS
// =============================================================================================
public SimpleArcLoader.STYLE getLoaderStyle() {
return mLoaderStyle;
}
public void setLoaderStyle(SimpleArcLoader.STYLE mLoaderStyle) {
this.mLoaderStyle = mLoaderStyle;
}
public int getArcMargin() {
return mArcMargin;
}
public void setArcMargin(int mArcMargin) {
this.mArcMargin = mArcMargin;
}
public int getAnimationSpeed() {
return mAnimationSpeed;
}
public void setAnimationSpeed(int mAnimationSpeed) {
this.mAnimationSpeed = mAnimationSpeed;
}
public void setAnimationSpeedWithIndex(int mAnimationIndex) {
switch (mAnimationIndex) {
case 0:
this.mAnimationSpeed = SimpleArcLoader.SPEED_SLOW;
break;
case 1:
this.mAnimationSpeed = SimpleArcLoader.SPEED_MEDIUM;
break;
case 2:
this.mAnimationSpeed = SimpleArcLoader.SPEED_FAST;
break;
}
}
public int getArcWidth() {
return mStrokeWidth;
}
public void setArcWidthInPixel(int mStrokeWidth) {
this.mStrokeWidth = mStrokeWidth;
}
public void setColors(int[] colors) {
if (colors.length > 0)
mColors = colors;
}
public int[] getColors() {
return mColors;
}
public void setTypeFace(Typeface typeFace) {
mTypeFace = typeFace;
}
public Typeface getTypeFace()
{
return mTypeFace;
}
public void setText(String mText)
{
this.mText = mText;
}
public String getText()
{
return mText;
}
public void setTextSize(int size)
{
mTextSize = size;
}
public int getTextSize()
{
return mTextSize;
}
public int getTextColor() {
return mTextColor;
}
public void setTextColor(int mTextColor)
{
this.mTextColor = mTextColor;
}
public boolean drawCircle()
{
return this.mArcCircle;
}
public void drawCircle(boolean drawCircle)
{
this.mArcCircle = drawCircle;
}
}
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SimpleArcDialog extends Dialog{
private SimpleArcLoader mLoaderView;
private ArcConfiguration mConfiguration;
private LinearLayout mLayout;
private TextView mLoadingText;
private boolean showWindow = true;
public SimpleArcDialog(Context context) {
super(context);
}
public SimpleArcDialog(Context context, ArcConfiguration configuration) {
super(context);
mConfiguration = configuration;
}
public SimpleArcDialog(Context context, int themeResId){
super(context, themeResId);
}
public SimpleArcDialog(Context context, int themeResId,ArcConfiguration configuration) {
super(context, themeResId);
mConfiguration = configuration;
}
public SimpleArcDialog(Context context, boolean cancelable, OnCancelListener cancelListener,ArcConfiguration configuration) {
super(context, cancelable, cancelListener);
mConfiguration = configuration;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.loader_layout);
mLoaderView = (SimpleArcLoader) findViewById(R.id.loader);
mLoadingText = (TextView) findViewById(R.id.loadertext);
mLayout = (LinearLayout) findViewById(R.id.window);
mLoadingText.setTextColor(Color.BLACK);
if (mConfiguration != null) {
mLoaderView.refreshArcLoaderDrawable(mConfiguration);
updateLoadingText(mConfiguration);
}
if(showWindow)
{
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.WHITE);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
mLayout.setBackgroundDrawable(gd);
if(mConfiguration != null && mConfiguration.getTextColor() == Color.WHITE)
mLoadingText.setTextColor(Color.BLACK);
}
}
private void updateLoadingText(ArcConfiguration configuration) {
String text = configuration.getText();
if (text.trim().length() == 0) {
mLoadingText.setVisibility(View.GONE);
} else {
mLoadingText.setText(configuration.getText());
}
Typeface typeface = configuration.getTypeFace();
if (typeface != null)
mLoadingText.setTypeface(typeface);
int textSize = configuration.getTextSize();
if (textSize > 0)
mLoadingText.setTextSize(textSize);
mLoadingText.setTextColor(mConfiguration.getTextColor());
}
public LinearLayout getLayout()
{
return mLayout;
}
public TextView getLoadingTextView()
{
return mLoadingText;
}
public void setConfiguration(ArcConfiguration configuration) {
mConfiguration = configuration;
}
public void showWindow(boolean state)
{
showWindow = true;
}
}
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
public class SimpleArcLoader extends View implements Animatable {
private static long FRAME_DURATION = 1000 / 60;
public static int MARGIN_NONE = 0;
public static int MARGIN_MEDIUM = 5;
public static int MARGIN_LARGE = 10;
public static int SPEED_SLOW = 1;
public static int SPEED_MEDIUM = 5;
public static int SPEED_FAST = 10;
public static int STROKE_WIDTH_DEFAULT = 10;
public enum STYLE {
SIMPLE_ARC, COMPLETE_ARC
}
ArcDrawable mArcDrawable;
public SimpleArcLoader(Context context) {
super(context);
}
public SimpleArcLoader(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public SimpleArcLoader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attributeSet) {
ArcConfiguration configuration = readFromAttributes(attributeSet);
// Load Drawable for this View
mArcDrawable = new ArcDrawable(configuration);
setBackgroundDrawable(mArcDrawable);
// Start the Animation
start();
}
private ArcConfiguration readFromAttributes(AttributeSet attributeSet) {
ArcConfiguration configuration = new ArcConfiguration(getContext());
TypedArray array = getContext().obtainStyledAttributes(attributeSet, R.styleable.SimpleArcLoader);
for (int i = 0; i < array.length(); i++) {
int type = array.getIndex(i);
if (type == R.styleable.SimpleArcLoader_arc_style) {
String value = array.getString(R.styleable.SimpleArcLoader_arc_style);
configuration.setLoaderStyle(STYLE.values()[Integer.parseInt(value)]);
}
if (type == R.styleable.SimpleArcLoader_arc_colors) {
int colors_resourse_id = array.getResourceId(R.styleable.SimpleArcLoader_arc_colors, 0);
if (colors_resourse_id != 0)
configuration.setColors(getContext().getResources().getIntArray(colors_resourse_id));
}
if (type == R.styleable.SimpleArcLoader_arc_speed) {
String value = array.getString(R.styleable.SimpleArcLoader_arc_speed);
if (value != null)
configuration.setAnimationSpeedWithIndex(Integer.parseInt(value));
}
if (type == R.styleable.SimpleArcLoader_arc_margin) {
Float value = array.getDimension(R.styleable.SimpleArcLoader_arc_margin, MARGIN_MEDIUM);
configuration.setArcMargin(value.intValue());
}
if (type == R.styleable.SimpleArcLoader_arc_thickness) {
Float value = array.getDimension(R.styleable.SimpleArcLoader_arc_thickness,
getContext().getResources().getDimension(R.dimen.stroke_width));
configuration.setArcWidthInPixel(value.intValue());
}
}
array.recycle();
return configuration;
}
@Override
public void start() {
if (mArcDrawable != null)
mArcDrawable.start();
}
@Override
public void stop() {
if (mArcDrawable != null)
mArcDrawable.stop();
}
@Override
public boolean isRunning() {
if (mArcDrawable != null)
return mArcDrawable.isRunning();
return false;
}
public void refreshArcLoaderDrawable(ArcConfiguration configuration) {
if (isRunning())
stop();
// Load Drawable for this View
mArcDrawable = new ArcDrawable(configuration);
setBackgroundDrawable(mArcDrawable);
// Start the Animation
start();
}
private class ArcDrawable extends Drawable implements Animatable {
final Runnable updater = new Runnable() {
@Override
public void run() {
mArcAnglePosition += mAnimationSpeed;
if (mArcAnglePosition > 360)
mArcAnglePosition = 0;
scheduleSelf(updater, FRAME_DURATION + SystemClock.uptimeMillis());
invalidateSelf();
}
};
ArcConfiguration mConfiguration;
Paint mPaint;
int mStrokeWidth, mArcMargin, mArcAnglePosition, mAnimationSpeed;
int mArcColors[];
boolean isRunning;
boolean mDrawCirle;
public ArcDrawable(ArcConfiguration configuration) {
mConfiguration = configuration;
initComponents();
}
private void initComponents() {
mStrokeWidth = mConfiguration.getArcWidth();
mArcMargin = mConfiguration.getArcMargin();
mArcColors = mConfiguration.getColors();
mAnimationSpeed = mConfiguration.getAnimationSpeed();
mDrawCirle = mConfiguration.drawCircle();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(mStrokeWidth);
mPaint.setStyle(Paint.Style.STROKE);
// Customize as per Style
if (mConfiguration.getLoaderStyle() == STYLE.SIMPLE_ARC) {
if (mArcColors.length > 1)
mArcColors = new int[] { mArcColors[0], mArcColors[0] };
}
}
@Override
public void start() {
if (!isRunning()) {
// Set the flag
isRunning = true;
scheduleSelf(updater, FRAME_DURATION + SystemClock.uptimeMillis());
invalidateSelf();
}
}
@Override
public void stop() {
if (isRunning()) {
// Set the flag
isRunning = false;
unscheduleSelf(updater);
invalidateSelf();
}
}
@Override
public boolean isRunning() {
return isRunning;
}
@Override
public void draw(Canvas canvas) {
int w = getWidth();
int h = getHeight();
int arc1_bound_start = mArcMargin + mStrokeWidth * 2;
int arc_padding = 0;
if (mDrawCirle) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.WHITE);
canvas.drawCircle(w / 2, h / 2, w / 2, mPaint);
// Reset the configuration
mPaint.setStyle(Paint.Style.STROKE);
// Add some padding
arc_padding += 3;
}
RectF arc1_bound = new RectF(arc1_bound_start + arc_padding, arc1_bound_start + arc_padding,
((w - (mStrokeWidth * 2)) - mArcMargin) - arc_padding,
((h - (mStrokeWidth * 2)) - mArcMargin) - arc_padding);
RectF arc2_bound = new RectF(mStrokeWidth + arc_padding, mStrokeWidth + arc_padding,
(w - mStrokeWidth) - arc_padding, (h - mStrokeWidth) - arc_padding);
int colors_length = mArcColors.length;
for (int i = 0; i < (colors_length > 4 ? 4 : colors_length); i++) {
int startangle = (90 * i);
mPaint.setColor(mArcColors[i]);
canvas.drawArc(arc1_bound, startangle + mArcAnglePosition, 90, false, mPaint);
canvas.drawArc(arc2_bound, startangle - mArcAnglePosition, 90, false, mPaint);
}
}
@Override
public void setAlpha(int i) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return 0;
}
}
}
loader_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/window"
android:background="@android:color/transparent"
android:layout_centerInParent="true"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp">
<com.example.simplearcdialog.SimpleArcLoader
android:id="@+id/loader"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height="60dp"
app:arc_style="simple_arc"/>
<TextView
android:id="@+id/loadertext"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:textSize="15dp"
android:layout_below="@+id/loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="Loading..."/>
</LinearLayout>
</RelativeLayout>
Maintivity使用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final SimpleArcDialog mDialog = new SimpleArcDialog(MainActivity.this);
//两种显示效果
SimpleArcDialog mDialog = new SimpleArcDialog(MainActivity.this);
mDialog.show();
}
}
自定义属性attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SimpleArcLoader">
<attr name="arc_style" format="enum">
<enum name="simple_arc" value="0"></enum>
<enum name="complete_arc" value="1"></enum>
</attr>
<attr name="arc_margin" format="dimension" />
<attr name="arc_thickness" format="dimension" />
<attr name="arc_speed" format="enum">
<enum name="slow" value="0"></enum>
<enum name="medium" value="1"></enum>
<enum name="fast" value="2"></enum>
</attr>
<attr name="arc_colors" format="reference"></attr>
</declare-styleable>
</resources>
定义样式
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item> 设置dialog的背景,此处为系统给定的透明值
<item name="android:windowFrame">@null</item> Dialog的windowFrame框为无
<item name="android:windowNoTitle">true</item> 是否显示标题
<item name="android:windowIsFloating">true</item> 是否浮现在activity之上
<item name="android:windowIsTranslucent">true</item> 是否半透明
<item name="android:windowContentOverlay">@null</item> 是否有覆盖
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 设置Activity出现方式
<item name="android:backgroundDimEnabled">true</item> 背景是否模糊显示
<item name="android:backgroundDimAmount">0</item> 这一句是设置背景的灰度,这里设置的是0,表示背景灰度完全透明!
</style>
效果图:
猛戳这里:eclipse点击下载
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12863.html