user5345059
user5345059

Reputation: 51

Custom loading spinner icon

I've implemented a loading spinner icon using the layout below:

<RelativeLayout
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_marginTop="50dp">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        />

</RelativeLayout>

But this uses the default Android spinner. I want to replace the spinner with a loading icon I got from preloaders.net that looks like this:

enter image description here

How can I replace the default Android spinner with this custom spinner?

Upvotes: 3

Views: 8774

Answers (3)

Giovanka Bisano
Giovanka Bisano

Reputation: 205

If you want to make your PNG image rotate and act as a Progress Bar, you can simply create a rotate layout and insert your PNG on that layout

Inside res ->drawable, create a new file called rotate.xml.

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/pokeball"       <--Insert your image here
    android:fillAfter="false"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:startOffset="0"
    android:toDegrees="718" />

After that you can insert your rotate.xml to a progress bar so that your PNG file able to rotate and you just need to maintain it's visibility

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:layout_margin="16dp"
    android:alpha="0.5"
    android:indeterminateDrawable="@drawable/rotate"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Upvotes: 4

injecteer
injecteer

Reputation: 20699

this is what I used in my project:

    <ProgressBar
        android:id="@+id/updateProgressBar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:drawingCacheQuality="high"
        android:indeterminateDuration="2000"
        android:indeterminateDrawable="@drawable/splash_spinner"
        />

simply override indeterminateDrawable with your custom image

Upvotes: 1

user2413972
user2413972

Reputation: 1355

I recommend taking advantage of this variant. To be honest, your spinner and not so cool to make it a 1 to 1.

If you can not find the normal solution. You can make your View.

class DrawThread extends Thread{
    private boolean runFlag = false;
    private SurfaceHolder surfaceHolder;
    private Bitmap picture;
    private Matrix matrix;
    private long prevTime;

    public DrawThread(SurfaceHolder surfaceHolder, Resources resources){
        this.surfaceHolder = surfaceHolder;

        picture = BitmapFactory.decodeResource(resources, R.drawable.spinner);

        matrix = new Matrix();
        matrix.postScale(3.0f, 3.0f);
        matrix.postTranslate(100.0f, 100.0f);

        prevTime = System.currentTimeMillis();
    }

    public void setRunning(boolean run) {
        runFlag = run;
    }

    @Override
    public void run() {
        Canvas canvas;
        while (runFlag) {
            long now = System.currentTimeMillis();
            long elapsedTime = now - prevTime;
            if (elapsedTime > 30){
                prevTime = now;
                matrix.preRotate(2.0f, picture.getWidth() / 2, picture.getHeight() / 2);
            }
            canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    canvas.drawColor(Color.BLACK);
                    canvas.drawBitmap(picture, matrix, null);
                }
            } 
            finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private DrawThread drawThread;

    public MySurfaceView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {   
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        drawThread = new DrawThread(getHolder(), getResources());
        drawThread.setRunning(true);
        drawThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        drawThread.setRunning(false);
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }
}

Upvotes: 0

Related Questions