Android Custom Seekbar with animation example
This post about how to create custom seekbar with animation.
I have created simple sample app for show mobile internal total and available memory with animation.
Xml Source Code
seekbar.xml
Activity Code
activity code contain get internal memory total and available size set to seekbar with animation .
Custom Seekbar View Class
This post about how to create custom seekbar with animation.
I have created simple sample app for show mobile internal total and available memory with animation.
Xml Source Code
seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.ramsizememorysize"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897959"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/refresh_match_layout"
android:layout_width="261dp"
android:layout_height="248dp"
android:layout_margin="5dp"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:id="@+id/free_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/volume_bar"
android:layout_centerHorizontal="true"
android:layout_marginLeft="43dp"
android:layout_marginTop="47dp"
android:textSize="10pt"
android:textColor="#f4bd4e"
android:textStyle="bold" />
<com.example.ramsizememorysize.HalfSeekabr
android:id="@+id/volume_bar"
android:layout_width="250dip"
android:layout_height="250dip"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:clickable="false"
android:secondaryProgress="0"
app:circle_x_radius="100"
app:circle_y_radius="100"
app:end_angle="270"
app:pointer_alpha_ontouch="100"
app:pointer_color="#0174DF"
app:pointer_halo_color="#880174DF"
app:start_angle="270"
app:use_custom_radii="true" />
</RelativeLayout>
<TextView
android:id="@+id/total_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:layout_weight="0.08"
android:textSize="10pt"
android:textColor="#403f3e"
android:textStyle="bold" />
</LinearLayout>
Activity Code
activity code contain get internal memory total and available size set to seekbar with animation .
package com.example.ramsizememorysize;
import java.io.File;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
final HalfSeekabr halfSeekBar = (HalfSeekabr) findViewById(R.id.volume_bar);
File path = Environment.getDataDirectory();
final StatFs stat = new StatFs(path.getPath());
final long blockSize = stat.getBlockSize();
final long totalBlocks = stat.getBlockCount();
HalfSeekabr.DEFAULT_MAX = safeLongToInt(blockSize * totalBlocks);
halfSeekBar.setMax(safeLongToInt(blockSize * totalBlocks));
halfSeekBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return true;
}
});
long availableBlocks = stat.getAvailableBlocks();
ValueAnimator anim = ValueAnimator.ofInt(0, safeLongToInt(blockSize
* availableBlocks));
anim.setDuration(6000);
final TextView textFree = (TextView) findViewById(R.id.free_text);
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animProgress = (Integer) animation.getAnimatedValue();
halfSeekBar.setProgress(animProgress);
textFree.setText(" Available \n " + animProgress + " MB");
}
});
anim.start();
TextView totalSize = (TextView) findViewById(R.id.total_size);
totalSize.setText("Total Internal Memory: "
+ getTotalInternalMemorySize());
}
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}
public static String getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return "Not Found";
}
}
public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return "Not Found";
}
}
public static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
}
public static int safeLongToInt(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
int valueInt = (int) size;
if (valueInt != size) {
throw new IllegalArgumentException("The long value " + size
+ " is not within range of the int type");
}
return valueInt;
}
}
Custom Seekbar View Class
package com.example.ramsizememorysize;
import java.io.File;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
final HalfSeekabr halfSeekBar = (HalfSeekabr) findViewById(R.id.volume_bar);
File path = Environment.getDataDirectory();
final StatFs stat = new StatFs(path.getPath());
final long blockSize = stat.getBlockSize();
final long totalBlocks = stat.getBlockCount();
HalfSeekabr.DEFAULT_MAX = safeLongToInt(blockSize * totalBlocks);
halfSeekBar.setMax(safeLongToInt(blockSize * totalBlocks));
halfSeekBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return true;
}
});
long availableBlocks = stat.getAvailableBlocks();
ValueAnimator anim = ValueAnimator.ofInt(0, safeLongToInt(blockSize
* availableBlocks));
anim.setDuration(6000);
final TextView textFree = (TextView) findViewById(R.id.free_text);
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animProgress = (Integer) animation.getAnimatedValue();
halfSeekBar.setProgress(animProgress);
textFree.setText(" Available \n " + animProgress + " MB");
}
});
anim.start();
TextView totalSize = (TextView) findViewById(R.id.total_size);
totalSize.setText("Total Internal Memory: "
+ getTotalInternalMemorySize());
}
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}
public static String getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return "Not Found";
}
}
public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return "Not Found";
}
}
public static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
}
public static int safeLongToInt(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
int valueInt = (int) size;
if (valueInt != size) {
throw new IllegalArgumentException("The long value " + size
+ " is not within range of the int type");
}
return valueInt;
}
}
Thanks! It worked ..
ReplyDeleteHow it is working for you ?
ReplyDelete