how do I drawline on canvas with a button press - java

The Title says it all. drawline works outside the setOnClick Listener, but not inside, so I can set it to run with a button press.
//Bitmap Object creation
bitmap = Bitmap.createBitmap(600,800,Bitmap.Config.RGB_565);
//Create Canvas object and set to bitmap variable
canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(50);
imageView.setImageBitmap(bitmap);
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
paint.setColor(Color.BLUE);
canvas.drawLine(100,100,100,200,paint);
}
});
Something else I want to look into is drawing multiple lines, and making sure each one lines up to the tip of the last one drawn, similar to this:
This will work for multiple buttons, but I only need it for one button right now. I hope this is sufficient information, please let me know if more is needed

Related

Android: Programatically rotating the text inside a button

{Visual Aid}
I am making an application with a drag and drop menu. User can fill out three edit texts (width, height, and rotation) then press on "add new" button and a new button with the specified values is created at the origin. However if the rotation attribute is anything other than 0 say 45 degrees, then it rotates the whole button along with the text. I would like for the text to remain horizontal with no rotation. I have looked up but the post all refer back to the rotation attribute which i am already using.
onViewCreated()
tvAddTable = view.findViewById(R.id.btn_add_table);
tvWidth = view.findViewById(R.id.et_width);
tvHeight = view.findViewById(R.id.et_height);
tvRotation = view.findViewById(R.id.et_rotation);
mSize = new Point();
mDisplay = Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay();
mDisplay.getSize(mSize);
final View.OnTouchListener touchListener = this;
tvAddTable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {[enter image description here][1]
ConstraintLayout layout = view.findViewById(R.id.floor_layout);
//set the properties for button
Button btnTag = new Button(getContext());
btnTag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if(ids.isEmpty()){
ids.add(0);
}else{
ids.add(ids.size());
}
btnTag.setId(ids.size());
btnTag.setText(Integer.toString(btnTag.getId()));
//add button to the layout
layout.addView(btnTag);
btnTag.setLayoutParams(new ConstraintLayout.LayoutParams(Integer.parseInt(tvWidth.getText().toString()), Integer.parseInt(tvHeight.getText().toString())));
btnTag.setRotation(Integer.parseInt(tvRotation.getText().toString()));
btnTag.setOnTouchListener(touchListener);
}
});
The rotation attribute rotates the entire view, not just the text or just the background. That's by design. There is no text or background rotation attribute. If you need that, make a custom view.
If you only want the background to rotate, you can probably do it by setting the background drawable to a RotateDrawable that wraps the background you want.

Unable to enter the if statement | Android Studio

I am trying to fade two images back and forth:
Image1 with an id of imageone
Image2 with an id of imagetwo
For both the images, I have linked onClick in UI properties to the onClick method in the below Java code.
After the launching the app, when I click on image1, it is supposed to fade out and image2 should fade IN.
After analysis, I determined that the flow is not entering the if statement and is always getting directed to else no matter what.
How can I solve this?
Why is the if statement if (view.getId() == R.id.imageone) not letting the flow inside it even though the entry condition view.getId() == R.id.imageone is true?
public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
ImageView imageone = (ImageView) findViewById(R.id.imageone);
ImageView imagetwo = (ImageView) findViewById(R.id.imagetwo);
if (view.getId() == R.id.imageone) {
imageone.animate().alpha(0f).setDuration(2000);//image1 fade OUT
imagetwo.animate().alpha(1f).setDuration(2000);//image2 fade IN
} else {
imagetwo.animate().alpha(0f).setDuration(2000);//image2 fade OUT
imageone.animate().alpha(1f).setDuration(2000);//image1 fade IN
}
}
}
#Narendra Jadon : You are right ,The problem was with the layout. The image two was on top of image one. And i solved it by setting the image one on top of image 2. Thank you everyone for your help.

How to assign an event to a drawable button?

I am trying to assign an event to a button, the button is declared as a Bitmap, I cannot seem to figure out how to add a listener to it, or even simply an event that I can call when the button is touched.
moregamesbtn = BitmapFactory.decodeResource(getResources(), R.drawable.moregames);
Bitmap moregamesbtn;
Edit - I attempted this, with no success;
moregamesbtn = BitmapFactory.decodeResource(getResources(), R.drawable.moregames);
ImageButton imgButton = (ImageButton) findViewById(R.drawable.moregames);
imgButton.setImageBitmap(moregamesbtn);
imgButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.e("MyApplication", "Pressing more games button");
}
});
You will have to assign your Bitmap to the Button background
button.setBackgroundDrawable(bdrawable);
Then make a onButtonClick Listener.
EDIT:
you might want to check this out as well:
http://www.mkyong.com/android/android-imagebutton-example/

Add an image as background and put other images on top of it

I am creating an image by using Canvas- and Bitmap class. I want to set it as a background for the user. Then I want to add some more images on top of it.
this is the code for image that is supposed to be as background.
ImageView imgMap1 = (ImageView) findViewById(R.id.imgMap1);
imgMap1.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bmp, 0, 0, 500, 500)));
and this is the code to make it as background:
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.nn);
this.setContentView(ll);
The Problem here is: When I set it as background, I can't see the other photo anymore.
How can I do this?
Thanks in advance.
The other Images are added in the Layout. they are movable by finger touch. user can reposition them by finger.
ImageView i1 = (ImageView) findViewById(R.id.Image1);
ImageView i2 = (ImageView) findViewById(R.id.Image2);
The layout is built from the top down in your XML file, or in the order you add elements in code. It sounds like your other images is being added to the layout first, either as a higher-up element in the XML file, or earlier in your code. You'll need to add the other code as context for a complete answer.
Just noticed that you can directly set Bitmap as your ImageView's content using setImageBitmap(Bitmap bm)See the Android Reference
Then let talk about your question.
First, create your own class extends the View;
Second, load background image and overlay image using Bitmap
Third, invoke onTouch event, so that the onDraw method will automatically redraw the overlay image using the coordinates returned by onTouch
Something like:
public class dragndrop extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// using this to load your background image
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565; // this is not a must
bmBackground = BitmapFactory.decodeFile(filePath, opt);
super.onCreate(savedInstanceState);
DrawView dv = new DrawView(dragndrop.this);
setContentView(dv);
}
public class DrawView extends View {
Point coordinate;
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
}
#Override
protected void onDraw(Canvas canvas) {
// assume you have already load your background image as bitmap
canvas.drawBitmap(bmBackground, 0, 0, null);
// assume bm is the overlay image you need to put on top,
// the method here will draw the object with the coordinate you give
canvas.drawBitmap(bm, coordinate.x, coordinate.y, null);
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
// add code here to determine the point user touched is within the object or not
break;
}
}
break;
case MotionEvent.ACTION_MOVE: // touch 'n' drag
// pass the touch point to your object
coordinate.x = x;
coordinate.y = y;
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
}
}
This is my own snippet, please edit before use, and please let me know when your question is resolved.

Problems drawing text onto an ImageView using a Canvas

I'm trying to implement a footer for my ListView which will act as a menu-type frame with five elements. I want the elements to be able to (dynamically, based on user input) display either images or text (or possibly both, but I'm not there yet). Eventually it will be more interactive but for now I was trying to implement it so that the ImageView (which by default displays an image) would erase the image and display text instead.
I thought the best way to implement this was to draw text on a canvas and use the android.view.View.draw(canvas c) method to draw it onto the ImageView. However, I'm having trouble doing this. Specifically, I can modify the view in almost any way (set the background color, change the drawable, etc etc) and it all works fine, and the code to draw the canvas compiles properly, but it doesn't do anything at runtime. The view remains completely unchanged.
(Note: I've tried to eliminate any stupid mistakes through code (i.e. made sure text wasn't transparent/same color as bg, made sure text was drawn inside the area of the view, etc) but I am not 100% it's error free.)
I tried both drawing to the view passed into the onClick function and drawing to a new ImageView constructed from the passed view - neither works.
Does anyone have any suggestions?
Code follows:
login_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Bitmap img = Bitmap.createBitmap( 60, 60, Config.ARGB_8888);
Canvas c = new Canvas();
c.setBitmap(img);
Paint myPaint = new Paint();
myPaint.setTypeface(mFace);
myPaint.setColor(android.R.color.black);
myPaint.setTextSize(2);
myPaint.setTextAlign(Paint.Align.LEFT);
String content = "testing";
c.drawText(content, 0,0, myPaint);
view.draw(c);//does nothing
ImageView view2 = (ImageView) view;
view2.setBackgroundColor(android.R.color.white); //this works perfectly
view2.draw(c);//does nothing
Toast.makeText(MainListView.this, ""+c.getHeight(), Toast.LENGTH_SHORT).show(); //making sure c has height - this returns 60 as expected
Toast.makeText(MainListView.this, "end of method", Toast.LENGTH_SHORT).show();
}
});
View.draw(Canvas) draws the View onto the Canvas, not vice versa like you seem to expect.
If you want to do it along the lines that you already are, you need to extend an existing View class, likely TextView or ImageView, and override onDraw(), as explained in the Custom Components Dev Guide.
I think what I would do is use something like a FrameLayout and set it to contain either the appropriate TextView or ImageView as necessary. That seems cleaner than manually rendering text in an ImageView.
You could create the Canvas from the Bitmap, and set the Bitmap as the Image for your ImageView.
Bitmap img = Bitmap.createBitmap( 60, 60, Config.ARGB_8888);
Canvas c = new Canvas(img);
Paint myPaint = new Paint();
myPaint.setTypeface(mFace);
myPaint.setColor(android.R.color.black);
myPaint.setTextSize(2);
myPaint.setTextAlign(Paint.Align.LEFT);
String content = "testing";
c.drawText(content, 0,0, myPaint);
ImageView view2 = (ImageView) view;
view2.setImageBitmap(img);
Toast.makeText(MainListView.this, ""+c.getHeight(), Toast.LENGTH_SHORT).show(); //making sure c has height - this returns 60 as expected
Toast.makeText(MainListView.this, "end of method", Toast.LENGTH_SHORT).show();
Although, I believe the "right" way to do this is to use an extension of View and override the onDraw method to draw differently based on some local variables:
login_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MyView)view).mContextVariable = true; //or false, etc
//you might not need this invalidate, because the click event probably causes and invalidate to be called
view.invalidate();
}
}
class MyView extends View
{
Paint myPaint;
boolean mContextVariable;
public MyView(Context context)
{
super(context);
myPaint = new Paint();
myPaint.setTypeface(mFace);
myPaint.setColor(android.R.color.black);
myPaint.setTextSize(2);
myPaint.setTextAlign(Paint.Align.LEFT);
}
#Override
protected void onDraw(Canvas canvas)
{
if(mContextVariable)
{
//draw something
}
else
{
//draw something else
}
canvas.drawText("testing", 0,0, myPaint);
}
}

Categories