Blurred bitmap on canvas - java

I have View-based element in main activity and an image in drawable folder
public class MyView extends View {
private Bitmap mSprite;
public MyView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
mSprite = BitmapFactory.decodeResource(getResources(), R.drawable.plank);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mSprite, 0.f, 0.f, null);
}
}
The image is displayed but looks blurry and too large (ref). How can I get the actual size of bitmap and draw in on canvas?

For anyone faced the above issue: just put your image in nodpi folder and it will match its actual size.

Related

canvas2Draw - how to connect view class to the canvas2

full code I'm new in android studio and I'm having problem with drawing.
Upon execution it does not work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View eissa=new eissa(this);
setContentView(eissa);
}
}
class eissa extends View {
private Canvas canvas2;
private Bitmap backingbitmap;
public eissa(Context context) {
super(context);
backingbitmap=Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
canvas2= new Canvas(backingbitmap);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(0,50,100,null);
canvas2.drawCircle(0,50,100,null);
canvas.drawBitmap(backingbitmap,0,0,null);
}
}
2.
There is a problem with the canvas.drawCircle() because there is no Paint argument (last one). The drawCircle doc says : "Paint: The paint used to draw the circle This value cannot be null."
So you can create a Paint in the constructor to pass to the drawCircle methods:
class eissa extends View {
private Canvas canvas2;
private Bitmap backingbitmap;
Paint viewPaint;
public eissa(Context context) {
super(context);
backingbitmap=Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
canvas2= new Canvas(backingbitmap);
viewPaint = new Paint();
viewPaint.setColor(0xFFFF0000); // set your desired color here context.getColor(R.color....);
viewPaint.setStrokeWidth(4);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(0, 50, 100, viewPaint);
canvas2.drawCircle(0, 50, 100, viewPaint);
canvas.drawBitmap(backingbitmap, 0, 0, null);
}
}
1.
The view's dimensions are not specified anywhere causing the platform to use the default LayoutParams which have the layout_width and layout_height equal to WRAP_CONTENT so the view is not visible. To address ui issues you can use Tools / Layout inspector which can help identify the cause (incorrect dimensions).
Here is a good tutorial about measuring custom views.
The quickest fix would be to specify dimensions using LayoutParams in setContentView:
// replace setContentView() in onCreate() with this
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(eissa, lp);

How to solve draw() it does not draw my screen

I am trying to make a gaze tracking in android studio. And I am trying to draw on screen a rectangle to the corresponding point of gaze. But my method doesn't work. I don't have any experience in drawing in android studio.
This is my draw class
public class drawMargins extends View {
Paint paint;
private volatile Margin margin;
public drawMargins(Context context) {
super(context);
init(null);
}
public drawMargins(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs);
}
public drawMargins(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public void updateMargin(Margin margin2) {
margin = margin2;
postInvalidate();
}
private void init(#Nullable AttributeSet set)
{
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (margin==null)
return;
paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawRect(
(float)((canvas.getWidth()/8)*margin.getX()),
(float)((canvas.getHeight()/8)*margin.getY()),
(float)((canvas.getWidth()/8)*margin.getX()+canvas.getWidth()/8),
(float)((canvas.getHeight()/8)*margin.getY()+canvas.getHeight()/8),paint);
}
}
and this is how I find the viewbyid("Draw") and update the vars:
DrawMargins = (drawMargins)LayoutInflater.from(context).inflate(R.layout.activity_live_preview,null).findViewById(R.id.Draw);
Margin eyeNose = new Margin(marginX(),marginY());
Margin drawOnScreen = onScreen(correspondingOnScreen(eyeNose,NEMargin,SEMargin,SWMargin,NWMargin,0),0,8,0,8,0 );
DrawMargins.updateMargin(drawOnScreen);
Should I call the draw method? With what canvas?
Try calling invalidate() instead of postInvalidate() in updateMargins(). This will call draw() and should draw the rectangle.
I would suggest you do one step at a time. I don't know exactly how you have your Activity or Fragment set, so I am just gonna go through that as well.
To keep things simple, let me assume that there is only Activity and no Fragments. You need two Java Class to get your objective accomplished: DrawMarginsActivity and DrawMargins.
DrawMargins is a Custom View. Custom views help you create your own custom drawing. Refer this for more details: https://developer.android.com/training/custom-views/custom-drawing . You need to override the onDraw(Canvas canvas) method. This method has the canvas parameter and you will be drawing on to this canvas.
Now you need to add DrawMargins view to your DrawMarginsActivity. You can do this by adding this view to your DrawMarginsActivity's layout file. Then you can get a reference to the DrawMargins view via findViewById().
Be default, when your Activity is created, the DrawMargins's onDraw() method will be called and it will render. But if you want to call the onDraw() again, you have to call DrawMargins.invalidate() method. This will invalidate the view and call the onDraw() method for you.
I am not sure on which action you want the view to draw. Based on that, you need to call the invalidate() method.

Android: How do I make a scrollable Canvas?

I am writing an app. One aspect of it is lines being drawn over an image. Here's the practice code I've been working with:
public class DrawView extends View {
Paint paint = new Paint();
private void init() {
paint.setColor(Color.BLACK);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
#Override
public void onDraw(Canvas canvas) {
paint.setStrokeWidth(20);
canvas.drawLine(100, 100, 20, 20, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}
}
I want the Canvas to be fairly large, so I need people to be able to scroll in both directions (left/right and up/down). How do I accomplish this? I'm unfamiliar with the Canvas class so any help will be appreciated.
If you want to draw on canvas with your custom height and width you have to call setContentView(android.view.View yourView , android.view.Viewgroup.LayoutParam yourLayout) in your activity class.Because by default setContentView(View view) method use full width and height.So you have to use its overloaded method with two parameter along with your desired. See documentation for more info.And don`t use only LayoutParams() constructor to create its object. Use it by writing its full path like android.view.ViewGroup.LayoutParams. Because there are some other classes with same name in Android SDK.
MyView customView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new MyView(getApplicationContext());
android.view.ViewGroup.LayoutParams lp = new android.view.ViewGroup.LayoutParams(100,200);//100 is width and 200 is height
setContentView(customView, lp);
customView.setOnClickListener(this);
}`

Bitmap create null pointer exception

Here is my xml file : Image view im trying to access
<ImageView
android:layout_width="350dip"
android:layout_height="400dip"
android:id="#+id/imgview"
android:background="#drawable/pattern"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Here is my code.. kindly review the code and let me know where i am doin things wrong i have searched all over the internet just cant try to figure it out how to make bitmap object and draw canvas on image view
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private ImageView img;
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
img = (ImageView)findViewById(R.id.imgview);
}
#Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.GREEN);
//I keep on getting null pointer exception here
Bitmap b = Bitmap.createBitmap(img.getMeasuredWidth(),img.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawRect(0, 0, 200, 200, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setTextSize(40);
paint.setTextScaleX(1.f);
paint.setAlpha(0);
paint.setAntiAlias(true);
c.drawText("Your text", 30, 40, paint);
paint.setColor(Color.RED);
canvas.drawBitmap(b, 10,10, paint);
}
}
}
If you're getting a NullPointerException on that line, it means img is null, which either means findViewById(R.id.imgview); returned null, or onDraw is being executed as part of super(context); (not sure if that's the case).
When width or height you want to have in your bitmap exceeds the size of device then you may get this error. You should use createScaledBitmap. This will help you.

Initializing Bitmap variable with bitmap drawable from resources is not working - using ANDROID

I use the following code to initialize my bitmap variable:
Bitmap bitmap = ((BitmapDrawable)this.model.gameView.context.getResources().getDrawable(R.drawable.pic1)).getBitmap();
When I try to log the width of that bitmap, the log does not even output anything for that call.
I know it's making it to that line, because I traced the code.
Also, when I try to do canvas.draw for the bitmap, nothing is drawn on the screen.
Everything I draw with rectangles works fine.
My picture resource is a PNG.
Any help is appreciated!
Try something like this for your bitmap class.
public class DrawBitmap extends View
{
Bitmap bitmap;
public DrawBitmap(Context content)
{
super(content);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);//whatever color you want, make sure it's not the same as your image
canvas.drawBitmap(bitmap, (canvas.getWidth()), 0, null);
}
}
Main Class
public class Main extends Activity
{
DrawBitmap myView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myView = new DrawBitmap(this);
setContentView(myView);
}
}
Try using BitmapFactory.decodeResource
Have a look at the answer in this topic:
How to convert a Drawable to a Bitmap?
Just figured it out. It had nothing to do with the method of bitmap loading I used.
It was a logical error on my part. My code accidentally reached a case where my bitmap became null, and it tried to draw the null resource on the canvas.

Categories

Resources