I make my custom view. and I also make my custom view (making Circle). But In my custom view, Alpha Animation only don`t work....
My Customview extends linearlayout
public class mylayout extends FrameLayout implements View.OnClickListener {
......
public mylayout(Context context){
super(context);
init(context);
}
void init(Context context){
this.setLayoutParams(new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mButton = new Button(getContext());
mButton.setText("Delete all Created Circle.");
mButton.setId(10);
mButton.setOnClickListener(this);
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.change_sea01);
this.addView(mButton,new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN) {
this.addView(new MyCircleView(getContext(), (int)event.getX(),
(int)event.getY()));
return true;
} else if(event.getAction() == MotionEvent.ACTION_MOVE) {
this.addView(new MyCircleView(getContext(),(int)event.getX(),
(int)event.getY()));
return true;
}
return super.onTouchEvent(event);
}
This is myCircle Class that make circle on touching point(x,y)
class MyCircleView extends ImageView{
.............
public MyCircleView(Context context, int xPos, int yPos) {
super(context);
pnt = new Paint();
pnt.setAntiAlias(true);
bDelete = false;
bmpCircle = BitmapFactory.decodeResource(context.getResources(),
R.drawable.img_click);
this.xPos = xPos;
this.yPos = yPos;
xWidth = bmpCircle.getWidth()/2;
yWidht = bmpCircle.getHeight()/2;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(setAnimation == null){
createAnimation(canvas);
}
canvas.drawBitmap(bmpCircle, xPos - xWidth, yPos - yWidht, pnt);
}
public void createAnimation(final Canvas canvas) {
AlphaAnimation alpha;
ScaleAnimation scale;
alpha = new AlphaAnimation(1.0f, 0.0f);
scale = new ScaleAnimation(1, 2, 1, 2, Animation.ABSOLUTE, xPos- xWidth,
Animation.ABSOLUTE, yPos - yWidht);
setAnimation = new AnimationSet(false);
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(scale);
setAnimation.setDuration(3000);
startAnimation(setAnimation);
}
}
Related
Canvas Painting is not reset when Click ,It is reset when I am drawing again on canvas after click the clear button.............................................................................................................................................................................................................................................................................................................................................................................
HERE IS MY CODE
public class Practice extends FragmentActivity {
private RelativeLayout relativeLayout;
private Paint paint;
ImageView back;
private View view;
private Path path2;
private Bitmap bitmap;
private Canvas canvas;
private Button button;
public int width, height;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
back = findViewById(R.id.back);
relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
button = (Button) findViewById(R.id.button);
view = new SketchSheetView(Practice.this);
paint = new Paint();
path2 = new Path();
relativeLayout.addView(view, new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
paint.setDither(true);
paint.setColor(Color.parseColor("#FFFFFF"));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.MITER);
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStrokeWidth(5);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
path2.reset();
}
});
}
class SketchSheetView extends View {
public SketchSheetView(Context context) {
super(context);
bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
canvas = new Canvas(bitmap);
this.setBackground(getResources().getDrawable(R.drawable.ic_brush_black_24dp));
}
private ArrayList<DrawingClass> DrawingClassArrayList = new ArrayList<DrawingClass>();
#Override
public boolean onTouchEvent(MotionEvent event) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
DrawingClass pathWithPaint = new DrawingClass();
canvas.drawPath(path2, paint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
this.setBackground(null);
vibrator.vibrate(500);
path2.moveTo(event.getX(), event.getY());
path2.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
this.setBackground(null);
path2.lineTo(event.getX(), event.getY());
vibrator.vibrate(500);
pathWithPaint.setPath(path2);
pathWithPaint.setPaint(paint);
DrawingClassArrayList.add(pathWithPaint);
}
invalidate();
return true;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (DrawingClassArrayList.size() > 0) {
canvas.drawPath(
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPath(),
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPaint()
);
}
}
}
public class DrawingClass {
Path DrawingClassPath;
Paint DrawingClassPaint;
public Path getPath() {
return DrawingClassPath;
}
public void setPath(Path path) {
this.DrawingClassPath = path;
}
public Paint getPaint() {
return DrawingClassPaint;
}
public void setPaint(Paint paint) {
this.DrawingClassPaint = paint;
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
In my application I design a custom view, and here is the code
public class Tunnel extends View implements View.OnTouchListener {
Paint paint = new Paint();
public Tunnel(Context context) {
super(context);
setOnTouchListener(this);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
for (int x = 0; x < canvas.getWidth(); x++) {
canvas.drawLine(x, (float) upperBound(x), x, (float) lowerBound(x), paint);
}
setBackgroundColor(Color.BLACK);
}
private double upperBound(double x) {
return 50 * Math.sin(x / 50) + 400;
}
private double lowerBound(double x) {
return 50 * Math.sin(x / 50) + 600;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
And it looks like this
Now what I need to do, is add a custom TextView on this view, which will show some text. As far as i realise, my constructor should look like this
public Tunnel(Context context) {
super(context);
setOnTouchListener(this);
TextView tv = new TextView(getContext());
tv.setX(200);
tv.setY(200);
//todo show the textView
}
But i don't know what to write next. How can i apply the textView to my view?
Thanks in advance
try
public Tunnel(Context context) {
super(context);
setOnTouchListener(this);
TextView tv = new TextView(getContext());
tv.setX(200);
tv.setY(200);
tv.setText("Text");
TheCustomView.addView(tv);
}
I inherited my custom view from a linear layout, set it to do the onDraw method, and it worked for me
public class Tunnel extends LinearLayout implements View.OnTouchListener {
Paint paint = new Paint();
TextView tv;
public Tunnel(Context context) {
super(context);
setOnTouchListener(this);
setWillNotDraw(false);
tv = new TextView(getContext());
tv.setX(450);
tv.setY(800);
tv.setTextSize(20f);
tv.setTextColor(Color.WHITE);
addView(tv);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
for (int x = 0; x < canvas.getWidth(); x++) {
canvas.drawLine(x, (float) upperBound(x), x, (float) lowerBound(x), paint);
}
setBackgroundColor(Color.BLACK);
}
private double upperBound(double x) {
return 50 * Math.sin(x / 50) + 400;
}
private double lowerBound(double x) {
return 50 * Math.sin(x / 50) + 600;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
I want to make my custom view be clicked.And now I do not know which is wrong,when I click it
,it happens nothing.Thanks in advance.This view is that I use canvas to draw a ring,and I want the inside of the ring can be clicked.
public class CircleProgressBar extends View{
OnClickListener progressButton;
private int hour;
private int maxProgress = 24;
private int progress;
int progress1;
private int progressStrokeWidth = 32;
RectF oval;
Paint paint;
Paint paint1;
public CircleProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
oval = new RectF();
paint = new Paint();
paint1 = new Paint();
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Calendar c=Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
progress = hour;
int width = this.getWidth();
int height = this.getHeight();
if(width != height){
int min = Math.min(width, height);
width = min;
height = min;
}
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setColor(Color.LTGRAY);
canvas.drawColor(Color.TRANSPARENT);
paint.setStrokeWidth(15);
paint.setStyle(Style.STROKE);
oval.left = progressStrokeWidth / 2; // 左上角x
oval.top = progressStrokeWidth / 2; // 左上角y
oval.right =height - progressStrokeWidth / 2; // 左下角x
oval.bottom = height - progressStrokeWidth / 2; // 右下角y
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
canvas.drawArc(oval, -90, 360, false, paint);
paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
paint.setStrokeCap(Paint.Cap.ROUND);
if(progress == 9){
canvas.drawArc(oval, -90, 134, false, paint);
}else{
canvas.drawArc(oval, -90, (long)(((float) progress / maxProgress) * 360), false, paint);
}
paint.setColor(Color.CYAN);
paint.setAntiAlias(true);
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
canvas.drawArc(oval, -90, 135+0.5f, false, paint);
paint.setStrokeWidth(15);
String text = (long)(((float) progress / maxProgress) * 100) + "%";
int textHeight = height / 4;
paint.setTextSize(textHeight);
int textWidth = (int) paint.measureText(text, 0, text.length());
paint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 2 +textHeight/2, paint);
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public void setProgress(int progress) {
this.progress = progress;
this.invalidate();
}
public void setProgressNotInUiThread(int progress) {
this.progress = progress;
this.postInvalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
}
return true;
}
This is my main activity.
public class MainActivity extends Activity{
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_fit);
CircleProgressBar progressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar);
progressBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();
}
});
here is my xml
<com.example.circlefit.CircleProgressBar
android:id="#+id/circleProgressbar"
android:layout_width="200dp"
android:layout_height="200dp" />
Remove the touch listener in your custom class. It is ovveriding the click functionality :-
/
/Remove this piece of code from your class and it will work just fine
#Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
}
return true;
}
Add this in your XML:
android:clickable="true"
android:focusable="true"
try like this:
private OnClickListener clickListener;
private boolean isMoved;
#Override
public void setOnClickListener(OnClickListener l) {
this.clickListener = l;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isMoved = false;
if (isValidtouchAndIsInsideCircle(event)) {
return true;
} else {
return false;
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
isMoved = true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (clickListener != null && !isMoved) {
clickListener.onClick(this);
}
isMoved = false;
}
return false;
}
private boolean isValidtouchAndIsInsideCircle(MotionEvent event) {
int xPoint = (int) event.getX();
int yPoint = (int) event.getY();
// here validate the x and y points with your circle coordinates if it
// is in circle return true or else return false
return false;
}
I'm making an Android app and I've got a tricky thing to do.
I need to draw a path on a canvas but the drawing should be animated (ie. drawing point after point with a slight delay).
Is it possible to make something like this using Android SDK?
If not, how could I produce this effect?
Try this code, I used it to draw a heartbeat using Path & Canvas:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new HeartbeatView(this));
}
public static class HeartbeatView extends View {
private static Paint paint;
private int screenW, screenH;
private float X, Y;
private Path path;
private float initialScreenW;
private float initialX, plusX;
private float TX;
private boolean translate;
private int flash;
private Context context;
public HeartbeatView(Context context) {
super(context);
this.context=context;
paint = new Paint();
paint.setColor(Color.argb(0xff, 0x99, 0x00, 0x00));
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setShadowLayer(7, 0, 0, Color.RED);
path= new Path();
TX=0;
translate=false;
flash=0;
}
#Override
public void onSizeChanged (int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
X = 0;
Y = (screenH/2)+(screenH/4)+(screenH/10);
initialScreenW=screenW;
initialX=((screenW/2)+(screenW/4));
plusX=(screenW/24);
path.moveTo(X, Y);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.save();
flash+=1;
if(flash<10 || (flash>20 && flash<30))
{
paint.setStrokeWidth(16);
paint.setColor(Color.RED);
paint.setShadowLayer(12, 0, 0, Color.RED);
}
else
{
paint.setStrokeWidth(10);
paint.setColor(Color.argb(0xff, 0x99, 0x00, 0x00));
paint.setShadowLayer(7, 0, 0, Color.RED);
}
if(flash==100)
{
flash=0;
}
path.lineTo(X,Y);
canvas.translate(-TX, 0);
if(translate==true)
{
TX+=4;
}
if(X<initialX)
{
X+=8;
}
else
{
if(X<initialX+plusX)
{
X+=2;
Y-=8;
}
else
{
if(X<initialX+(plusX*2))
{
X+=2;
Y+=14;
}
else
{
if(X<initialX+(plusX*3))
{
X+=2;
Y-=12;
}
else
{
if(X<initialX+(plusX*4))
{
X+=2;
Y+=6;
}
else
{
if(X<initialScreenW)
{
X+=8;
}
else
{
translate=true;
initialX=initialX+initialScreenW;
}
}
}
}
}
}
canvas.drawPath(path, paint);
//canvas.restore();
invalidate();
}
}
}
It uses drawing a Path point by point with couple of effects using counters. You can take what you need and transfer it to SurfaceView which is more efficient.
I hope this is what you are looking for. It draws the path on user touch, you could simply tweek it to achieve what you desire.
public class MyCanvas extends Activity implements OnTouchListener{
DrawPanel dp;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dp = new DrawPanel(this);
dp.setOnTouchListener(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(30);
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
fl.addView(dp);
setContentView(fl);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
dp.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dp.resume();
}
public class DrawPanel extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false ;
public DrawPanel(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
#Override
public void run() {
// TODO Auto-generated method stub
while( isItOk == true){
if(!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 0, 0, 0);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
synchronized(pointsToDraw)
{
for (Path path : pointsToDraw) {
canvas.drawPath(path, mPaint);
}
}
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
synchronized(pointsToDraw)
{
if(me.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(me.getX(), me.getY());
//path.lineTo(me.getX(), me.getY());
pointsToDraw.add(path);
}else if(me.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(me.getX(), me.getY());
}else if(me.getAction() == MotionEvent.ACTION_UP){
//path.lineTo(me.getX(), me.getY());
}
}
return true;
}
}
I have made it with ObjectAnimator.
We have any Path and our CustomView (in wich we'll draw our path)
private CustomView view;
private Path path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.custom_view);
path = new Path();
path.moveTo(0f, 0f);
path.lineTo(getResources().getDimension(R.dimen.point_250), 0f);
path.lineTo(getResources().getDimension(R.dimen.point_250), getResources().getDimension(R.dimen.point_150));
findViewById(R.id.btnStart).setOnClickListener(v -> {
test();
});
}
private void test() {
ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "xCoord", "yCoord", path);
pathAnimator.setDuration(5000);
pathAnimator.start();
}
And just pass our "xCoord" and "yCoord" to CustomView
public class CustomView extends View {
private Paint paint;
private float xCoord;
private float yCoord;
private Path path = new Path();
public void setXCoord(float xCoord) {
this.xCoord = xCoord;
}
public void setYCoord(float yCoord) {
this.yCoord = yCoord;
path.lineTo(xCoord, yCoord);
invalidate();
}
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(20);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
}
This might help... It draws adjacent circles instead of a path to simulate an animatable path.
public class PathAnimatable {
private final float CIRCLE_SIZE = 2.5f;
public float SPPED_SCALE = 1f;
private float steps = 0;
private float pathLength;
private PathMeasure pathMeasure;
private float totalStepsNeeded;
private float[] point = new float[]{0f, 0f};
private float stride;
public PathAnimatable() {
this(null);
}
public PathAnimatable(Path path) {
super(path);
init();
}
private void init() {
pathMeasure = new PathMeasure(path, false);
pathLength = pathMeasure.getLength();
stride = CIRCLE_SIZE * 0.5f;
totalStepsNeeded = pathLength / stride;
steps = 0;
}
#Override
public void setPath(Path path) {
super.setPath(path);
init();
}
// Called this from your locked canvas loop function
public void drawShape(Canvas canvas, Paint paint) {
if (steps <= pathLength) {
for (float i = 0; i < steps ; i += stride) {
pathMeasure.getPosTan(i, point, null);
canvas.drawCircle(point[0], point[1], CIRCLE_SIZE, paint);
}
steps += stride * SPPED_SCALE;
} else {
steps = 0;
}
}
}
I have a MenuView ViewGroup which capsules all the buttons of my interface.
On start of the app all buttons are positioned via button.layout(). Then I hide all buttons, with one exception: the menu button. When I am pressing the menu button, all other buttons are shown (via a zoom animation).
Now the problem: while positioning the buttons via button.layout(), they are visible. So almost 1 sec all buttons are shown on startup of the app.
How can I avoid that?
My button code:
public class CircleButton extends Button {
static final int StateDefault = 0;
static final int StateFocused = 1;
static final int StatePressed = 2;
private Bitmap mBitmapDefault;
private String mCaption;
protected int radius;
protected int color = 0xff000000;
private Typeface font1;
private boolean visible = true;
private int savedLeft, savedTop;
// indicates whether the button is visible when the menu is rendered
public boolean visibleInMenu = true;
private Paint paint;
private static String TAG = "CircleButton";
public int getRadius(){
return radius;
}
public CircleButton setRadius(int radius){
this.radius = radius;
this.setWidth(2*radius);
this.setHeight(2*radius);
return this;
}
public CircleButton setColor(int color){
this.color = color;
return this;
}
public CircleButton setCaption(String caption){
mCaption = caption;
return this;
}
public CircleButton(Context context) {
super(context);
init(context);
}
public CircleButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CircleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
setClickable(true);
setBackgroundColor(0x00000000);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
font1 = Typeface.createFromAsset(context.getAssets(), "fonts/Bebas/BEBAS___.ttf");
mCaption = "Caption";
this.setRadius(45);
this.setTextSize(18);
setOnClickListener(onClickListener);
setOnTouchListener(onTouchListener);
}
public void setVisibility(int visibility) {
if(visibility == GONE){
this.visible = false;
}else{
this.visible = true;
}
super.setVisibility(visibility);
}
/**
* draws the normal button (without rollover)
* #param canvas
*/
protected void drawDefault(Canvas canvas){
//canvas.drawARGB(255, 255, 0, 0);
Paint paintText = new Paint();
paintText.setAntiAlias(true);
paintText.setTextSize(this.getTextSize());
paintText.setColor(0xffffffff); // white
paintText.setTypeface(this.font1);
Rect bounds = new Rect();
//Paint textPaint = this.getPaint();
paintText.getTextBounds(mCaption,0,mCaption.length(),bounds);
float left = (float) (2*radius-bounds.width())/2;
float top = (float) radius+bounds.height()/2;
//Log.v("Button","bounds:"+bounds.width()+"x"+bounds.height());
//Log.v("Button","this.getWIdth:"+2*radius);
// create the Drawing Tool (Brush)
Paint paint = new Paint();
paint.setAntiAlias(true); // for a nicer paint
paint.setColor(color);
paint.setStrokeWidth(1);
paint.setStyle(Style.FILL);
Path path = new Path();
path.addCircle(radius, radius, radius, Path.Direction.CW);
canvas.drawPath(path, paint);
canvas.save();
canvas.rotate(-45,this.getRadius(),this.getRadius());
canvas.drawText(mCaption, left, top, paintText);
canvas.restore();
}
protected Bitmap getDefaultBitmap(){
if(mBitmapDefault == null){
mBitmapDefault = Bitmap.createBitmap(2*radius, 2*radius, Config.ARGB_8888);
Canvas canvas = new Canvas(mBitmapDefault);
this.drawDefault(canvas);
return mBitmapDefault;
}
return mBitmapDefault;
}
#Override
protected void onDraw(Canvas canvas) {
//Log.v("Button","onDraw(): "+this.getWidth()+"x"+this.getHeight());
super.onDraw(canvas);
canvas.drawBitmap(this.getDefaultBitmap(), 0, 0, paint);
//super.onDraw(canvas);
//canvas.drawBitmap(this.getDefaultBitmap(), new Rect(0, 0, this.radius*8, this.radius*8), new Rect(0,0, this.radius*2, this.radius*2), null);
}
public void recycle() {
if(mBitmapDefault != null){
mBitmapDefault.recycle();
mBitmapDefault = null;
}
}
public void hide(){
this.hide(true);
}
public void hide(boolean withAnimation){
if(this.visible == true){
savedLeft = getLeft();
savedTop = getTop();
ScaleAnimation anim = new ScaleAnimation(1, 0, 1, 0, this.getRadius(), this.getRadius());
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
visible = false;
layout(0, 0, 0, 0);
}
});
this.startAnimation(anim);
}
}
public void show(){
if(this.visible == false){
this.setVisibility(VISIBLE);
OvershootInterpolator inter = new OvershootInterpolator();
ScaleAnimation anim = new ScaleAnimation(0, 1, 0, 1, this.getRadius(), this.getRadius());
anim.setDuration(300);
anim.setInterpolator(inter);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
visible = true;
}
});
this.startAnimation(anim);
this.layout(this.savedLeft, this.savedTop, this.savedLeft+2*this.radius, this.savedTop+2*this.radius);
}
this.visible = true;
}
public CircleButton setOnClickCallback(OnClickListener l) {
super.setOnClickListener(l);
return this;
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
}
};
private OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//Log.v("Button","onTouch()");
if(event.getAction() == MotionEvent.ACTION_DOWN){
OvershootInterpolator inter = new OvershootInterpolator();
ScaleAnimation anim = new ScaleAnimation(1, (float) 1.5, 1,(float) 1.5, CircleButton.this.getRadius(), CircleButton.this.getRadius());
anim.setInterpolator(inter);
anim.setDuration(200);
anim.setFillAfter(true);
CircleButton.this.startAnimation(anim);
}else if(event.getAction() == MotionEvent.ACTION_UP){
ScaleAnimation anim = new ScaleAnimation((float) 1.5, 1, (float) 1.5, 1, CircleButton.this.getRadius(), CircleButton.this.getRadius());
anim.setDuration(300);
anim.setFillAfter(true);
CircleButton.this.startAnimation(anim);
}
return false;
}
};
}
Use setVisibility(int visibility) method on the object to hide it with Parameter visibility One of VISIBLE, INVISIBLE, or GONE:
GONE
This view is invisible, and it doesn't take any space for layout purposes. Use with setVisibility(int) and android:visibility.
Constant Value: 8 (0x00000008)
INVISIBLE
This view is invisible, but it still takes up space for layout purposes. Use with setVisibility(int) and android:visibility.
Constant Value: 4 (0x00000004)
VISIBLE
This view is visible. Use with setVisibility(int) and android:visibility.
Constant Value: 0 (0x00000000)
Try putting android:visibility="invisible" to those buttons in your layout xml.