Custom View into a Toast - doesn't draw - android

i'm trying to show a Toast message with a custom view i created.
the view has a bitmap on the background and i want to write some text on it.
if I assign the bitmap to an ImageView on the main code I manage to make it show up with Toast t; (...) t.show();
but when it's the onDraw() method of my class to assign the bitmap nothing shows up.
i checked, and my view has a size of (0, 0) when created the way i transcribe under.
help please.
Main.java
Toast t = new Toast(this);
LimitView lv = new LimitView(this);
t.setView(lv);
t.setDuration(Toast.LENGTH_LONG);
t.show();
LimitView.java
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.save();
canvas.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.limit));
canvas.restore();
}

try this code:
Main.java
Context context = this;
Toast t = new Toast(context);
LeftBorder lv = new LimitView(context);
t.setView(lv);
t.setDuration(Toast.LENGTH_LONG);
t.show();
LimitView.java
public class LimitView extends View {
public LimitView (Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button_pressed), getMatrix(), new Paint());
}
}

There are several errors here.
BitmapFactory.decodeResource(getResources(), R.drawable.limit)
This is a heavy operation and should not be on view drawing phase.
canvas.setBitmap();
This method does not drawing bitmap on canvas but setting the canvas buffer to use this bitmap.
Try some thing like this:
public class MyView extends View{
private Bitmap bitmap;
public MyView(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.limit);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
}
}

replace this
canvas.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.limit));
to this:
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button_pressed),left,top, new Paint());
here left and right are the position of the screen.

Related

Android dynamic Bubbles on the view

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));
}
}

Android bitmap mask clear doesn't stick

I have a class called MaskView:
public class MaskView extends View
{
private Context context;
public Bitmap imageMask;
public int maskXPos;
public int maskYPos;
private Paint maskPaint;
public MaskView(Context context)
{
super(context);
this.context = context;
maskXPos = 0;
maskYPos = 0;
maskPaint = new Paint();
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// 90% alpha
this.setBackgroundColor(Color.argb(230, 0, 0, 0));
}
#Override public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.save();
if(imageMask != null)
{
canvas.drawBitmap(imageMask, maskXPos, maskYPos, maskPaint);
}
canvas.restore();
}
}
At the moment, I got an Activity that fades in my mask using alpha fade in animation.
During the animation, I see my black mask overlay with the circular cutout bitmap mask.
However, after the mask view finish animating in, the bitmap mask instead of staying transparent, it gets rendered, and so I see a black circular bitmap instead of circular hole in my Mask view.
Is there a way to get the bitmap mask to remain a bitmap mask, and not get rendered?
I found the solution...after a few hours :D
Needed one line extra:
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I don't know what the implications are for setting the layer type to software, but on my Galaxy Nexus, the view is rendering fine with animation. Doesn't seem to be any bad performance here.
Here's full code:
public class MaskView extends View
{
private Context context;
public Bitmap imageMask;
public int maskXPos;
public int maskYPos;
private Paint maskPaint;
public MaskView(Context context)
{
super(context);
setClickable(true);
this.context = context;
maskXPos = 0;
maskYPos = 0;
maskPaint = new Paint();
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
// ------------------------------------------------------
// This line is needed for the mask to work
// ------------------------------------------------------
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
#Override public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.argb(230, 0, 0, 0));
if(imageMask != null)
{
canvas.drawBitmap(imageMask, maskXPos, maskYPos, maskPaint);
}
}
}
I discovered the solution after trying out this tutorial:
https://medium.com/#rgomez/android-how-to-draw-an-overlay-with-a-transparent-hole-471af6cf3953#.7uxgln7n4
Hope it helps others.

canvas.getDrawingCache() returning null - Android

I have separate class for canvas ViewCanvas in this class I'm drawing two worlds. Here is the code:
public class ViewCanvas extends View {
private Paint paint;
private Typeface typeFace;
public ViewCanvas(Context context) {
super(context);
}
public ViewCanvas(Context context, String first, String second) {
super(context);
setDrawingCacheEnabled(true);
buildDrawingCache(true);
paint = new Paint();
typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/BigNoodleTitling.ttf");
bounds = new Rect();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
paint.setTypeface(typeFace);
// Closing hardware acceleration
setLayerType(LAYER_TYPE_SOFTWARE, paint);
// Drawing first word
canvas.save();
.
.
.
.
// Drawing second world
canvas.restore();
.
.
.
.
}
}
This code works fine. I'm using this ViewCanvas in MainActivity like this:
public class MainActivity extends Activity {
private ViewCanvas viewCanvas;
private Bitmap imageForShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewCanvas = new ViewCanvas(this, first, second);
LinearLayout llCanvas = (LinearLayout) findViewById(R.id.llCanvas);
llCanvas.addView(viewCanvas);
imageForShare = viewCanvas.getDrawingCache();
}
Here imageForShare is null, and I don'n know why.
In my onClick method, viewCanvas.getDrawingCache() it's working great. imageForShare is not null and I can use it. Here is my code:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFacebook:
imageForShare = viewCanvas.getDrawingCache();
break;
}
}
Where is the problem, and I want imageForShare to be available in my onCreate method.
try this:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
link
Can you not use OnStart() to get the screenshot?
The visible lifetime of an activity happens between a call to onStart()
OK, I found a solution. The creating of imageForShare in onCreate method is impossible because canvas is drawing after imageForShare is created. So i put creation of imageForShare in onWindowFocusChanged, and is working great:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
imageForShare = viewCanvas.getDrawingCache();
}

android- how to change the color of canvas in a view?

I have written a customized View which draws a circle(it's onDraw function has been overridden to do that).
Now how doI change the color of my circle, from code?(from the Activity function which is going to show that circle)
You can make a setCircleColor to change the color of the circle and call invalidate that will call the View onDraw method.
You can also check for invalidate(Drawable drawable).
public class MyCustomView extends View {
MyCustomView myView;
private Paint myCircle;
public MyCustomView(Context context){
super(context);
initView();
}
private void initView(){
myView = this;
myCircle = new Paint();
myCircle.setColor(0xa300ff00);
}
#Override
protected void onDraw(Canvas canvas) {
drawCircle(canvas);
}
private void drawCircle(Canvas canvas){
canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, 10, myCircle);
}
public void setCircleColor(int color){
myCircle.setColor(color);
myView.invalidate();
}
}

Passing canvas to a function in onDraw method

There are two classes: Renderer which extends View and Grid. In the onDraw method of Renderer, I want to pass Canvas to the function of Grid. Since Java passes arguments only by value the code below does not work. I mean it does not draw a rectangle on the screen and says that program has stopped on my phone. Any idea?
public class Renderer extends View {
Paint paint;
Grid grid;
public Renderer(Context context, Grid grid) {
super(context);
paint = new Paint();
this.grid = grid;
}
protected void onDraw (Canvas canvas) {
canvas.drawRGB(255, 255, 255);
grid.tempName(canvas); // removing this solves problem
invalidate();
}
}
public class Grid {
Paint paint;
public void tempName (Canvas canvas) {
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
canvas.drawRect(100, 100, 200, 200, paint);
}
}

Categories

Resources