I am following a tutorial, and have the following code, but I want to modify it a little bit, so I want when its clicked inside the circle it must be filled with another collor, how I can do it, as I am just learning this concept I dont have any idea of how it can be done :/
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
}
you have to add a touch listener on your view
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myView = new MyView(this);
setContentView(myView);
myView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
//if(x==... your code here
invalidate(); //this repaints the view
return false;
}
});
}
}
Related
I want to create an android app that draw on current layout of app without setting any other Contentview. This is the code that I have did but in this code it is creating an other view and then allow to draw but not on current layout.
**Canva Class:**
public CanvaClass(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
path = new Path();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float xPosition = event.getX();
float yPosition = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(xPosition,yPosition);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPosition,yPosition);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
}
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CanvaClass canvaClass = new CanvaClass(getApplicationContext(),null);
setContentView(canvaClass);
}
});
}
}
I'm pretty new to programming so it might be that I've gotten the whole idea wrong here but..
I have two classes, other is the main class (Main) and the second (DrawingLines) is just plain one. I'm trying to call an onDraw method from the DrawingLines in my main class.
I did a bit of research but all i found was to use
DrawingLines draw = new DrawingLines ();
draw.onDraw
And I think this is how it's supposed to be but I just cant figure out what parametres I should use. You can see the parametres I've been trying to use in the example below, but AndroidStudio just gives "Cannot resolve symbol".
Main class:
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent ev) {
text.setText("Touch at " + (int)ev.getX() + ", " + (int)ev.getY());
return true;
}
});
}
DrawingLines draw = new DrawingLines(Context context);
draw.onDraw(canvas);
}
DrawingLines class:
class DrawingLines extends View {
public DrawingLines(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect pallo = new Rect();
pallo.set(0, 0, canvas.getWidth(), canvas.getHeight() / 2);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(pallo, blue);
}
}
Your code is not in a method:
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent ev) {
text.setText("Touch at " + (int)ev.getX() + ", " + (int)ev.getY());
return true;
}
});
DrawingLines draw = new DrawingLines(this.getApplicationContext());
draw.onDraw(canvas);
}//onCreate
}//class main
First of all, if you want to draw something you don't have to force it programmatically. DrawingLines draw = new DrawingLines(Context context);
draw.onDraw(canvas)
Put your view DrawingLines inside layout R.layout.activity_main and create variable use findViewById.
Then you can perform drawingLines.invalidate() on that view, so the View will be redrawn (method void onDraw(Canvas canvas) will be called).
I created a simple project that can draw by finger (I put all staff that performs draw in custom view)
DrawView:
public class DrawView extends View {
private final static String LOG = "CUSTOM_LOG";
private Paint paint;
private Path path = new Path();
public DrawView(Context context) {
this(context, null);
}
public DrawView(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
Log.d(LOG, "init");
setOnTouchListener(onTouchListener);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(15);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d(LOG, "onDraw");
canvas.drawPath(path, paint);
}
private OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getX();
float y = motionEvent.getY();
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(LOG, "ACTION_DOWN");
path.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
Log.d(LOG, "ACTION_MOVE");
path.lineTo(x, y);
break;
case MotionEvent.ACTION_UP:
Log.d(LOG, "ACTION_UP");
path.reset();
break;
}
invalidate();
return true;
}
};
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.gitlab.onreg01.simpledrawexample.DrawView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"/>
</FrameLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
project repository
In order to position a button in the exact spot on all devices, I have created it with a custom view. The button takes up a small part of the screen but the actual view takes up most of the screen. All the area around the button is transparent. How can I allow the user to click through the transparent area to buttons underneath but still click the button area on the view?
Here is the relavant code:
public class MainActivity extends AppCompatActivity {
final float dotScale = 0.3f;
Dot dot1, dot2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.mainView);
MyView myView = new MyView(this);
myLayout.addView(myView);
// Two dots are created.
dot1 = new Dot(this);
dot1.xOffset = 2.9f;
dot1.yOffset = 3.3f;
myLayout.addView(dot1);
dot2 = new Dot(this);
dot2.xOffset = -2.4f;
dot2.yOffset = 1.1f;
myLayout.addView(dot2);
// Makes dots clickable
dot1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!dot1.isClicked) {
dot1.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
.scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
dot1.isClicked = true;
}
}
});
dot2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!dot2.isClicked) {
dot2.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
.scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
dot2.isClicked = true;
}
}
});
}
.......
//custom view for Dots
class Dot extends View {
int radius;
float xOffset;
float yOffset;
boolean isClicked = false;
public Dot(Context context) {
super(context);
setClickable(true);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
double ratio = (547d / 828d);
float circleX = (float)((x / 2) - (y * ratio) / xOffset);
float circleY = (float)(y / yOffset);
radius = (int)((float)y/13);
setPivotX(circleX);
setPivotY(circleY);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(circleX, circleY, radius, paint);
}
}
You should set your view clickable and don't override onTouch at all or just return false from it. Return false means that you didn't consume event and it will be forwarded to the next element in view hierarchy.
I'm kinda new to building apps on Android. I do have some Java developing experience, but the project I do now is first on Android.
I'm building a basic cards game, and I want the card to move to the specific location I touch.
I did it, and the card moves to the touch point, but once the animation is over it bounces back to starting position.
I added a Image re-position code, but now it begins the animation from that point.
any ideas, anyone?
the code I did is:
public class FinallyActivity extends Activity {
/** Called when the activity is first created. */
EditText DBG;
ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBG = (EditText) findViewById(R.id.editText1);
iv = (ImageView) findViewById(R.id.imV1);
}
private void RunAnimations(MotionEvent event) {
// Animation b = AnimationUtils.loadAnimation(this,R.anim.cardtrans);
int xStart, yStart, dx, dy;
xStart = iv.getLeft();
yStart = iv.getTop();
Animation b = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getX()-50,
Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getY()- 105);
b.setDuration(3000);
iv.clearAnimation();
iv.startAnimation(b);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
RunAnimations(event);
}
iv.layout((int)event.getX()-25,(int) event.getY()-105,
(int)event.getX()+25,(int) event.getY()-35);
return true;
}
}
Thanks All!!
b.fillAfter(true)
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.subview);
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
xDown = event.getX();
yDown = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float moveX,moveY;
moveX = event.getX();
moveY = event.getY();
float distanceX = moveX -xDown;
float distanceY = moveY -yDown;
imageView.setX(imageView.getX()+distanceX);
imageView.setY(imageView.getY()+distanceY);
break;
}
return true;
}
});
}
This code works only for the first animation. Though the final position of the ImageView is not exactly the point of the MotionEvent and the animation looks not smooth. The result of all other MotionEvents is actually no animation. The ImageView just appears at the point i touch the screen.
Here is the code.
ImageView image;
MyAnimationListener am;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
image = (ImageView)findViewById(R.id.imageView1);
am = new MyAnimationListener(image);
}
#Override
public boolean onTouchEvent(MotionEvent mEvent){
if(mEvent.getAction()==MotionEvent.ACTION_DOWN){
TranslateAnimation tAnimation = new TranslateAnimation(image.getX(),mEvent.getX(),image.getY(),mEvent.getY());
tAnimation.setDuration(500);
am.setCoordinates(mEvent.getX(), mEvent.getY());
tAnimation.setAnimationListener(am);
AnimationSet aSet = new AnimationSet(true);
aSet.addAnimation(tAnimation);
image.startAnimation(aSet);
}
return true;
}
And the interesting methods of MyAnimationListener.
ImageView i;
float x,y;
public MyAnimationListener(ImageView i){
this.i = i;
}
public void setCoordinates(float x, float y){
this.x = x;
this.y = y;
}
#Override
public void onAnimationEnd(Animation animation) {
i.setX(x);
i.setY(y);
}
Thanks in advance.
may be you need to do call a fillAfter so that the postion is not reset at the end of the animation. see link