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);
Related
I've been browsing the entire internet, but I could not find any answer to this question. I want to create a background, some image buttons, along with a bunch of moving graphics (circles). Since I do not know how to overwrite a xml layout with moving graphics, I chose to customly create my view, draw the background, the imageButtons and the circles (2D graphics).
public class GameView extends View implements UtilConstants
since I extended the View class, I had to call the super class constructor in my constructor
GameView(Context context) {
super(context);
this.context = context;
this.paint = new Paint();
}
and to implement the onDraw method, which acts like the java paintComponent
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // necessary for some reason
setGridBackground(); // sets the background
paint.setColor(Color.parseColor("#FF0000"));
canvas.drawCircle(this.x += 10, 300, 20, paint); //moves my graphics (ball) within the screen
drawScoreInLeftUpperCorner(canvas, 20); // paints a string text on the screen
setAbilitiesImages(); // places some imageButtons
try {
Thread.sleep(THREAD_REFRESH_RATE_MS);
} catch (InterruptedException ex) {
System.out.println(ex);
}
invalidate();
}
Now, to get to my problem:
I do not know how to set those ImageButtons !!! I am trying
private void setAbilititesImages() {
ImageButton imageButton = new ImageButton(this.context); // is this ok to put the argument this.context??
imageButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageButton.setImageResource(R.drawable.monster); // set my resource
RelativeLayout relativeLayout = findViewById(R.id.in_game_layout); // what is the purpose of this???
if (relativeLayout != null) { // it never enters this if, relativeLayout is always null
relativeLayout.addView(imageButton);
}
}
Aaaaand the image button never shows up...why is it necessary to use Relative/Linear layouts? That R.id.in_game_layout I created is just an empty RelativeLayout xml. Can't I just use something like
imageButton.setImageResource(R.drawable.monster); // set my resource
imageButton.setBounds(.....);
(View)this.add(imageButton); ???
ImageButton requires a parent view to be hosted in it. The parent View can be a RelativeLayout, a linearlayout or a ConstraintLayout.
In your case, the instance relativeLayout is always null because you not specify the view parent in which you do findViewById.
You should make something like :
RelativeLayout relativeLayout = view.findViewById(R.id.in_game_layout);
You need to add layout param to your parent Relative layout also to display your Image button appropriately.
Example
RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ImageButton imageButton = new ImageButton(this);
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_background,null));
relativeLayout.addView(imageButton,params);
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);
}`
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.
I am struggling with the logic behind this, so any tips would be appreciated. I have two classes, one in which the fragment is created and one in which my custom view is created, and I am trying to do some animation. In the main layout, I have an edit text field and a button. What I want to do is to add to the custom view when a button is clicked, the text within the edit text will be added to the custom view. Thinking about HOW to do this, I am drawing a blank, and I am beginning to think it is not possible. Should I just create the edit text within the custom view? Here is the code showing what I am doing (but stuck on a next step, or whether or not I should scrap this approach and try a different one)
The main fragment
public class DestroyerView extends Fragment
{
private Context mContext;
Paint paint = new Paint();
private AnimatedNegative PositiveAnimatedNegative;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this.getActivity();
View view = inflater.inflate(R.layout.activity_destroyer, container, false);
final Button fire = (Button) view.findViewById(R.id.destroy);
PositiveAnimatedNegative = (AnimatedNegative) view.findViewById(R.id.anim_view);
fire.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// logic here?
}
});
return view;
}
}
public AnimatedNegative(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = this.getContext();
h = new Handler();
mCalendarDbHelper=new CalendarDbAdapter(mContext);
mCalendarDbHelper.open();
}
private Runnable r= new Runnable()
{
#Override
public void run()
{
invalidate();
}
};
protected void onDraw (Canvas canvas)
{
String word = "This is a sentence";
paint.setColor(Color.parseColor("#1E90FF"));
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
x = this.getWidth()/2;
y = this.getHeight()/2;
canvas.drawText(word, x, y, paint);
}
}
... Now this would be much easier if I could reference the button inside of the custom view, but it doesn't seem I can do that, or if I could simply add to the custom view inside of the main activity class, but it doesn't seem that I can do that either (not without creating a new canvas, which seems to be doing too much for what it is that I want to do (just add the words)). So, finally, is the way I am currently trying to do this a dead end? Any help would be appreciated.
I think I got it. I'll call invalidate with the the AnimatedNegative object defined in my main activity, and then pass in the word from the edit text and a true value, so that I can check for it in the view and redraw. Added code shown below:
Within the main activity:
public void onClick(View arg0)
{
PositiveAnimatedNegative.invalidate();
PositiveAnimatedNegative.add = true;
PositiveAnimatedNegative.positive_word = "This is a positive word";
}
And within the custom view:
if (add == true)
{
canvas.drawText(positive_word, x,y, paint);
}
First of all, thanks in advance for any help you can provide. This has been driving me nuts and google's been no friend of mine for this.
I've created a DrawView so the user can paint something in the app. What I'd like to do is place that DrawView within a layout XML file, so I can place a TextView title on the top and two buttons on the bottom, one of which will save what the user painted as an image file. So far, all I've been able to do is create an area for the user to paint in. Is there any way to place this DrawView view into an XML file? If not, what alternative solutions do I have?
Also, this is an optional question as I'm sure I'll find the solution by googling, at some point, but is there a better way to smoothly draw along the user's finger movement, other than the canvas.drawLine() method I am currently using?
public class Draw extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
}
public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";
ArrayList <Float> nikpointx = new ArrayList<Float>();
ArrayList <Float> nikpointy = new ArrayList<Float>();
Bitmap nikbmp;
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStrokeWidth(4);
}
#Override
public void onDraw(Canvas canvas) {
//I'm doing this, because the drawCircle method places circles at different points
//in the screen, rather than drawing smooth lines. I haven't found a more elegant
//solution yet :-(
canvas.drawLine(nikpointx.get(i-1), nikpointy.get(i-1), nikpointx.get(i), nikpointy.get(i), paint);
canvas.setBitmap(nikbmp);
}
}
public boolean onTouch(View view, MotionEvent event) {
// if(event.getAction() != MotionEvent.ACTION_DOWN)
// return super.onTouchEvent(event);
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
nikpointx.add(event.getX());
nikpointy.add(event.getY());
return true;
}
}
You can place you custom view in the xml layout like this:
<full.package.to.customview.DrawView android:id="#+id/idOfCustom"
//other attributes
/>
You can also add custom attributes to your view to set directly from the xml file.
I don't have any solutions for your second problem.
edit: after a quick search on this site i found this -> Android How to draw a smooth line following your finger