Can anyone how to make dynamic bubble on the Android layout which is clickable.
My designer thought for the screen is below [![I the image all the bubble are some set of task assigned to the user.The label of bubble changes according to the task ][1]][1]
According to my project requirement the color and radius will change as per the api response.
Can you please suggest any demo or example. I googled it but i cant find the answer for this. Please guide me to accomplish this .
As one answer is already posted, I also tried for you. Hope you get some help from here too :
public class BubbleBackgroundDemoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = new CustomView(this);
// RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(this.getWidth(),
// ViewGroup.LayoutParams.MATCH_PARENT);
// view.setLayoutParams(lp);
setContentView(view);
}
public class CustomView extends View {
private Paint paint;
int screenWidth, screenHeight;
public CustomView(Context context) {
super(context);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenWidth = displaymetrics.widthPixels;
screenHeight = displaymetrics.heightPixels;
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.GRAY);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawCircle(200, 200, 100, paint);
canvas.drawCircle(screenWidth-200, 200, 100, paint);
canvas.drawCircle(screenWidth/2, screenHeight/2, 300, paint);
canvas.drawCircle(screenWidth-200, screenHeight-200, 100, paint);
canvas.drawCircle(200, screenHeight-200, 100, paint);
}
}
}
This is how customize circle created you can refere various links to create circle on canvas dynamically
public class CustomView extends View {
private Paint paint;
public CustomView(Context context) {
super(context);
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.GRAY);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawCircle(200, 200, 100, paint);
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this));
}
}
Related
I have to make a gradient color effect in the view that generated dynamically according to the scenario and also the view would be of any shape (diagonal or square)
As shown in image, gradient effect could be in any shape.
Also, if I create custom view for every possible case and play with Visibility, then how I will manage these views to fit perfectly on every device screen size?
Just need a small help to start.
Thanks in advance.
I have solved the issue using this approach.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this));
}
}
and now create a CustomView class
public class CustomView extends View {
Rect rect;
private Rect rectangle;
private Paint paint;
public CustomView(Context context) {
super(context);
int x = 50;
int y = 50;
int sideLength = 200;
int sideLength1 = 100;
rectangle = new Rect(x, y, sideLength, sideLength1);
paint = new Paint();
paint.setColor(Color.GRAY);
}
#Override
protected void onDraw(Canvas canvas) {
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
BitmapShader shader;
//shader = new BitmapShader(header, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//paint.setShader(shader);
Path path = new Path();
path.moveTo(40,40);
path.lineTo(5,height/2);
path.lineTo(width/2,height/4);
path.lineTo(width/2,0);
canvas.drawPath(path,paint);
}
I want to add xml layout to custom view. It is done in commented section of code. But I am not able to draw after doing that. This Code paint red circle in center of screen. After uncommenting commented lines circle is not painted.
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
Paint paint;
public MyView(Context context) {
super(context);
// View view = inflate(context, R.layout.activity_main_zadanie, null);
// view.setFocusable(true);
// addView(view);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
}
Your new view is probably covering the canvas on your custom view. Try setting the background to translucent in R.layout.activity_main_zadanie.
I have a view and I want to draw a shape on it (circle for example) after click.
I've tried to do this but there are two problems -
onDraw is never called.
Not sure the setLayoutParams(v.getLayoutParams) will give me the result I want.
#Override
public void onClick(View v) {
CircleView circle = new CircleView(GameXoActivity.this, v.getWidth(), v.getHeight());
circle.setLayoutParams(v.getLayoutParams());
circle.startDrawing();
}
CircleView:
public CircleView(Context context, int width, int height) {
super(context);
this.width = width;
this.height = height;
}
protected void startDrawing() {
this.postInvalidate();
}
#Override
protected void onDraw(Canvas canvas) {
Log.d("TAG", "onDraw");
// draw circle
}
}
}
UPDATE:
The shape is not an image and I want to draw it with animation (I didn't write the entire code).
Also, the shape is not always a circle, so using a drawable-state is not an option.
Because there is not just one view, but 9, I don't think the making 9 more on top of them would be right.
As I'm sure you'll need to customize this quite a bit, I've left things rather generic. The following example will animate a blue circle being drawn clockwise, starting from the east (0 degrees), on top of the View's content when the View is clicked.
public class CircleView extends View
{
private static final int MARGIN = 50;
Handler handler = new Handler();
Paint paint = new Paint();
RectF rect = new RectF();
boolean drawing = false;
float sweep = 0;
public CircleView(Context context)
{
this(context, null);
}
public CircleView(Context context, AttributeSet attrs)
{
super(context, attrs);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(15);
paint.setColor(Color.BLUE);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawArc(rect, 0, sweep, false, paint);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
rect.set(MARGIN, MARGIN, w - MARGIN, h - MARGIN);
}
public void startAnimation()
{
drawing = true;
handler.post(runnable);
}
Runnable runnable = new Runnable()
{
#Override
public void run()
{
sweep += 10;
if (!(sweep > 360))
{
invalidate();
handler.postDelayed(this, 20);
}
else
{
drawing = false;
sweep = 0;
}
}
};
}
In this Activity example, I used an image that most developers would already have in their project, but it can obviously be changed to your custom image. Also, for the sake of simplicity and brevity, the CircleView is set as the entire content of the Activity, but it can easily be listed in an xml layout, as well.
public class MainActivity extends Activity
{
CircleView circle;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
circle = new CircleView(this);
circle.setBackgroundResource(R.drawable.ic_launcher);
circle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
circle.startAnimation();
}
}
);
setContentView(circle);
}
}
I suggest, create two imageView with same dimensions, set the image you want to display on image view and then make the second image invisible .
For example :
circleimage.setVisibility(View.INVISIBLE);//now its hidden(do it OnCreate)
and then show 2ndimage when 1stimage is clicked(do it in onclick of 1st image)
circleimage.setVisibility(View.VISIBLE);
If you want to mess with drawing of the object you should overridepublic void draw(Canvas canvas) and not protected void onDraw(Canvas canvas)
EDIT:Please read comments, this first statement of my answer is probably wrong
but I would use a FrameLayout or a RelativeLayout and put the images one on top of another.
Then you can play with the visibility of the overlaying image in order to hide/show it.
EDIT:
In case your circle is not an image and needs to be drawn, make your own circle class extending View and use it as a component in the FrameLayout or RelativeLayout as you would do if it were an image
I am doing animation, some translate animation using but this not what I want I have given the reference of an app Front screen with animation...having three bubbles .initially they will be all together and after onTouch() it just opens up translating to its x,y coordinates
This is the class i am using to translate but i know lots of work has to be done
My Draw view class-
public class DrawView extends View {
Paint paint = new Paint();
private int height;
Bitmap circlegreen ,circlered,circleyellow;
public DrawView(Context context, int height) {
super(context);
this.height = height;
circlegreen=BitmapFactory.decodeResource(getResources(), R.drawable.circlegreen);
circlered=BitmapFactory.decodeResource(getResources(), R.drawable.circlered);
circleyellow=BitmapFactory.decodeResource(getResources(), R.drawable.circleyellow);
this.height= this.height+circleyellow.getHeight();
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawBitmap(circlegreen, 30, height, paint);
canvas.drawBitmap(circlered, 30, height, paint);
canvas.drawBitmap(circleyellow, 30, height, paint);
// canvas.drawRect(30, height, 60, 300, paint );
}
And in the fragment i am adding this DrawView to container..
#SuppressLint("NewApi")
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.fragment_home, container, false);
//dineInBtn=(Button) view.findViewById(R.id.dinein);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
height = size.y;
Log.v("width=", width+"");
Log.v("height=", height+"");
final CountDownTimer timer = new CountDownTimer(2000, 50) {
#Override
public void onTick(long millisUntilFinished) {
height = height - 10;
drawView = new DrawView(getActivity(), height);
drawView.setBackgroundColor(Color.WHITE);
container.addView(drawView);
}
#Override
public void onFinish() {
}
};
Runnable run=new Runnable() {
#Override
public void run() {
timer.start();
}
};
new Thread(run).start();
Also attaching the image..
Can use arc menu library :
here is library
Its ARC Menu Library you can find the sample example below
https://github.com/daCapricorn/ArcMenu
I am trying to achieve gradient text as told by one of the member in stack flow
Below is my MainActivity class where I calling my Draw2d class which draws the canvas
public class TextEffectsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView secondTextView = new TextView(this);
secondTextView.setText(R.id.textView6);
Draw2d d = new Draw2d(this, secondTextView);
// setContentView(R.layout.main);
setContentView(d);
}
here is my draw2d class Where check the end of the code for gradient I am using shader
public class Draw2d extends View{
TextView secondTextView;
public Draw2d(Context context, TextView tv) {
// TODO Auto-generated constructor stub
super(context);
secondTextView=tv;
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(R.color.VeryLightGrey);
Paint p = new Paint();
// For gradient in text
Shader textShader=new LinearGradient(0, 0, 0, 0,new int[]{Color.YELLOW,Color.CYAN},
null, TileMode.CLAMP); // null or new float[]{0.5f,1.0f}
secondTextView.getPaint().setShader(textShader);
}
i think you should do that before drawing color,
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
Shader textShader=new LinearGradient(0, 0, 0, 0,
new int[]{Color.YELLOW,Color.CYAN},
null, TileMode.CLAMP); // null or new float[]{0.5f,1.0f}
secondTextView.getPaint().setShader(textShader);
canvas.drawColor(R.color.VeryLightGrey);
// For gradient in text
}