Resize canvas to path or resize path to canvas - android

Is there a way to resize my canvas to fit the width of a path, or is there a way to resize a path to fit a canvas.
IF both are possible which would be easier and how?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
demoview = new DemoView(this);
}
public class DemoView extends View implements OnTouchListener{
Paint paint = new Paint();
public DemoView(Context context){
super(context);
}
public void onDraw(Canvas canvas) {
canvas.drawPath(Plotter.path, paint);
}
}

Somewhat artificial, but if you use your path as a clipping path you may be able to then obtain its bounds by calling getClipBounds, and from there you can easily calculate the scale transformation needed to make it resize to fit a canvas.
There's got to be a better way though.

Related

How can I draw text to a view in a circular pattern in Android

I want to add text in a circle or half circle programmatically, in such a way that instead of having a circle with line edges, the edges are the words. See image for a better explanation.
How can I do this in Android, or what resources could I read in order to help me with this problem?
In order to do this, you will need to draw your text onto a Canvas. Any subclass of View is passed a Canvas in onDraw() that you can use to draw your custom text. The method drawTextOnPath() lets you put text on any Path object you choose. You can create a semi-circle path by creating a new instance and using addArc().
you can Use Below Code. and Make it as you Want your Textview.
Here if you want Something as Backgroung image then use setBackgroundResource(R.drawable.YOUR_IMAGE);
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsView(this));}
static public class GraphicsView extends View {
private static final String QUOTE = "text in a half-circle";
private Path circle;
private Paint cPaint;
private Paint tPaint;
public GraphicsView(Context context) {
super(context);
int color = Color.argb(127, 255, 0, 255);
circle = new Path();
circle.addCircle(230, 350, 150, Direction.CW);
cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
cPaint.setStyle(Paint.Style.STROKE);
cPaint.setColor(Color.LTGRAY);
cPaint.setStrokeWidth(3);
// For Background Image
setBackgroundResource(R.drawable.YOUR_IMAGE);
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//TextColor you want to set
tPaint.setColor(Color.BLACK);
//TextSize you want to set
tPaint.setTextSize(50);}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);}
}
}
try it out. hope it will help you.

Drawing with a canvas over an ImageView

I want to create an ImageView and then draw text on top of the ImageView. I also need to be able to modify the text periodically. Currently I've created a custom view that extends ImageView. Then I overwrite onDraw() and use it to draw the text. Only problem then is that when I use my custom ImageView it doesn't draw the image, just the text.
public class BoardView extends ImageView
{
public BoardView(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
Paint paint = new Paint();
setImageResource(R.drawable.board);
paint.setColor(Color.BLUE);
canvas.drawText(x.getName(), x.getX(), x.getY(), paint);
}
}
You should call the super.onDraw in your onDraw method before your draw code.

Is there a way to extract a Canvas or Bitmap from a View?

I have this class that extends View and draws a line:
public class MyDraw extends View
{
Paint paint = new Paint();
public MyDraw(Context context)
{
super(context);
paint.setColor(Color.BLUE);
}
#Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(1, 1, 100, 100, paint);
}
}
I would like to use the existing view from the Context to draw on top of it. Is it possible?
If you are just trying to get the view as a bitmap, you can get it from the drawing cache. This should work.
view.buildDrawingCache;
Bitmap bm = view.getDrawingCache
You cannot use the exisitng view instance and add it again as it already has a parent assigned to it and it wil cause an exception.

Scale image within custom ImageView

I'm extending ImageView in order to manually scale an image within the view. I want to scale an image to fill the width of the custom view, and then draw it to the canvas, however, I'm unable to get the view width using this.getWidth()
It just returns 0, as the view has not yet been drawn and so has dimensions 0 by 0.
Currently I have the following in my main.xml:
<com.android.myapp.BackgroundView
android:id="#+id/background_view"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/background"
android:dither="true"
/>
The custom class is as follows:
public class BackgroundView extends ImageView {
private Paint paint;
private Bitmap background;
public BackgroundView(Context context) {
super(context);
paint = new Paint();
loadBitmap();
}
public BackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
loadBitmap();
}
public void loadBitmap() {
BitmapDrawable src = (BitmapDrawable) this.getDrawable();
background = src.getBitmap();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(background, 0, 0, paint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
My Main.java class is:
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
I can't use
Bitmap.createScaledBitmap(background, view.getWidth(), view.getHeight(), false)
as the view hasn't yet been drawn, how would I go about scaling the image to fill the view/screen width at this point?
Thanks in advance.
I believe what you want to do could also be achieved by using ImageView directly in conjunction with the scaleType attribute. Either use fitXY or centerCrop, depending on your needs.
But to answer the question, you can only use getWidth() and getHeight() after layout() has been called. So you should be able to use the values inside your onDraw method.
Also you could use another drawBitmap method so you wouldn't have to create a new bitmap in memory.

How to take canvas on imageview in android

I want to display canvas contents on imageview in android
i am not understanding the
imageview.draw(canvas);
Heres my code:
public class Matrix extends Activity {
public Bitmap mybitmap,newbmp,bitmap,bmp;
ImageView imageview;
Paint paint;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.ImageView01);
imageview.setDrawingCacheEnabled(true);
}
protected void onDraw(Canvas canvas)
{
imageview.draw(canvas);
mybitmap=BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(mybitmap, 0, 0, paint);
}
}
"I want to display canvas contents on imageview in android"
So you want to draw what is on the Canvas in to your ImageView? If that's what you want then you need to read the links given by johike because you seem to have become confused a little.
The following in your code:
imageview.draw(canvas);
Does NOT mean draw the contents of canvas in to the imageview. It means the opposite, draw the imageview to the canvas.
Even if your question is not detailed enough to give you a precise answer I can give you the following hints:
Derive your on class from ImageView and then override the onDraw method
#Override
protected void onDraw(Canvas canvas) {
// draw a blue background
canvas.drawColor(Color.BLUE);
// additional drawings here
}
Further study the Android references:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/reference/android/graphics/Canvas.html

Categories

Resources