Hi I'm doing a practice project which could draw a line with the coordinates I put. The screen only have two textfields and one button. For example if I put "20" and "30" in those two textfields and click the "draw" button, I want the app to draw a line from (0,0) to (20,30) in ANOTHER VIEW. The problem is when I click the button, the values in those two textfields are passed into the setCoordinates() function, but the view just doesn't show the line. I tried change coordinates[0] and coordinates[1] to 50 and 50 in the canvas.drawLine() function then the line shows up, so I guess may be the invalidate() function is not working? Anyway please help find where my problem is, thanks!!!!! Here is my code:
MazeBuilder.java
package com.example.maze;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Color;
public class MazeBuilder extends Activity {
private DrawMaze drawMaze ;
private EditText editTextX;
private EditText editTextY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawMaze = new DrawMaze(this);
setContentView(R.layout.activity_maze_builder);
editTextX = (EditText) findViewById(R.id.editText1);
editTextY = (EditText) findViewById(R.id.editText2);
}
public void buildMaze(View view){
final int x = getValue(editTextX);
final int y = getValue(editTextY);
drawMaze.setCoordinates(x, y);
}
private static int getValue (EditText text) {
try {
return Integer.parseInt(text.getText().toString());
} catch (NumberFormatException ex) {
return 0;
}
}
}
DrawMaze.java
package com.example.maze;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawMaze extends View{
private Paint paint = new Paint();
private int[] coordinates = new int[2];
//===============================Constructors==============================================
//
//
public DrawMaze(Context context) {
super( context );
}
public DrawMaze(Context context, AttributeSet attrs) {
super( context, attrs );
}
public DrawMaze(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
}
//===============================Initialize the color of line and background==========================================
//
//
public void init(){
paint.setColor(Color.BLACK);
this.setBackgroundColor(Color.WHITE);
}
public void setCoordinates(int x, int y){
coordinates[0] = x;
coordinates[1] = y;
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
System.out.println("=============coordinates[0]:"+coordinates[0]+"================coordinates[1]:"+coordinates[1]+"========");
canvas.drawLine(0, 0, coordinates[0] , coordinates[1] , paint);
}
}
activity_maze_builder.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MazeBuilder" >
<com.example.maze.DrawMaze
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout1" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="500dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="Columns: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="buildMaze"
android:text="Build" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textView1"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toLeftOf="#+id/editText1"
android:text="Rows: "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
instead of
drawMaze = new DrawMaze(this);
use
drawMaze = (DrawMaze)findViewById(R.id.relativeLayout2);
but under
setContentView()
the reason is that when you set the content view with your layout, it is using the DrawMaze from your layout and not the
drawMaze = new DrawMaze(this);
which was instantiated but never set into the content view
Related
I am making a simple LED colour control app which sends data over bluetooth to an arduino which then controls an LED strip.
I am trying to implement a colour wheel, then use getPixel to pick a colour to send to the arduino. I am having lots of issues getting the bitmap to draw correctly and work. I am testing using the Android Studio preview and using my phone (HTC U11) to test on hardware.
I cannot get this image to draw correctly. Here is my canvas.
package com.example.buledin.ledstripcontrolapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
//we must extend View for simplicity
public class DrawableCanvas extends View {
static Canvas c;
private Paint BlueEdgePaint = new Paint();
private Paint BlackEdgePaint = new Paint();
ColourPoint point;
private Bitmap colourWheelB;
Rect rect;
public DrawableCanvas(Context context) {
super(context);
init(null, null, 0);
this.setWillNotDraw(false);
}
public DrawableCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public DrawableCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, 0);
}
// This is the initialisation routine.
private void init(Context context, AttributeSet attrs, int defStyle) {
BlueEdgePaint.setColor(Color.BLUE);
BlueEdgePaint.setStrokeWidth(4);
BlueEdgePaint.setStyle(Paint.Style.STROKE);
BlueEdgePaint.setAntiAlias(true);
BlackEdgePaint.setColor(Color.BLACK);
BlackEdgePaint.setStrokeWidth(2);
BlackEdgePaint.setStyle(Paint.Style.STROKE);
BlackEdgePaint.setAntiAlias(true);
// colourWheel = getResources().getDrawable(R.drawable.color_wheel_730_no_outline);
colourWheelB = BitmapFactory.decodeResource(context.getResources(), R.drawable.color_wheel_730_no_outline);
point = new ColourPoint(0, 0);
invalidate();
}
// Here is our DRAW LOOP.
#Override
public void onDraw(Canvas c) {
//draw
DrawableCanvas.c = c;
super.onDraw(c);
//draw wheel
rect = new Rect(0, 0, c.getWidth(), c.getHeight());
c.drawBitmap(colourWheelB, 0, 0, null);
//selection circle
c.drawCircle(point.getX(), point.getY(), 20, BlueEdgePaint);
c.drawCircle(c.getWidth() / 2, c.getHeight() / 2, c.getHeight() / 2, BlackEdgePaint);
}
public void updatePoint(int x, int y) {
point.setNewCoords(x, y);
invalidate();
}
public static double distanceFromCentre(double x, double y) {
double A = Math.abs(x - (c.getWidth() / 2));
double B = Math.abs(y - (c.getHeight() / 2));
double distance = Math.sqrt(A * A + B * B);
return distance;
}
public int[] getRGBfromPoint(double x, double y ) {
int pixel = colourWheelB.getPixel((int) x ,(int) y);
int Red = Color.red(pixel);
int Green = Color.green(pixel);
int Blue = Color.blue(pixel);
int[] ret = {Red, Green, Blue};
Log.v("Coords: ", ""+point.getX() + " " + point.getY());
Log.v("Color:", "Red = " + Red + " " + "Green = " + Green + "Blue = " + Blue);
return ret;
}
}
And here is my Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.buledin.ledstripcontrolapp.MainActivity"
tools:showIn="#layout/activity_main">
<SeekBar
android:id="#+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="22dp"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="10dp"
android:progress="100" />
<Button
android:id="#+id/strobebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="73dp"
android:text="Strobe" />
<Button
android:id="#+id/rainbowbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/strobebutton"
android:layout_centerHorizontal="true"
android:text="RAINBOW" />
<Button
android:id="#+id/offbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/rainbowbutton"
android:text="Off" />
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="Null" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/seekBar"
android:layout_alignParentStart="true"
android:gravity="center"
android:text="Brightness"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/rainbowbutton"
android:text="Status:" />
<TextView
android:id="#+id/brightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="100" />
<com.example.buledin.ledstripcontrolapp.DrawableCanvas
android:id="#+id/canvas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_below="#+id/strobebutton"
android:color="#66000000" />
</RelativeLayout>
Here is what it looks like on the phone
And here is what it looks like in the Android studio preview
Note the scaling is all wrong for both the colourwheel and the "outline circle" which I draw over the top for aesthetic reasons. I don't know why the scaling is all wrong, but when I use the app on my phone in hardware testing I can essentially only set colours shown in the first screenshot so I lose out on 3/4 of the wheel.
Here's the real kicker though and the real head-scratcher : Despite what the image looks like on screen ( I have got it to successfully draw on my phone as a full circle) I can still only Select colours as if only that top left 1/4 of the wheel was there.
I have the following fragment. No matter what I do but the image is not visible in the Image View-: For testing I put the button on fragment but that is refelected but image is not visible
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.transenigma.iskconapp.DynamicImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/img2"
android:id="#+id/myAboutImg" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
tools:text="Text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal|bottom" />
<!--</android.support.v7.widget.CardView>-->
</FrameLayout>
I have made the following class that extends ImageView-:
package com.transenigma.iskconapp;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class DynamicImageView extends ImageView {
public DynamicImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final Drawable d = this.getDrawable();
if (d != null) {
// ceil not round - avoid thin vertical gaps along the left/right edges
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth());
this.setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
What did you want to do in your DynamicImageView?
You can try to delete the method onMeasure,and see the difference between them.
Maybe the mistake is in your onMeasure.
I am trying to create a tree graph based on user data. There is no library or anything to help and im just a beginner. I just created 8 image buttons and after the app calculates the data, it needs to draw lines between these nodes to connect them. I have no idea how to use canvas or drawline so any help would be much appreciated.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:id="#+id/nodesImages">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="1"
android:layout_margin="20dp"
android:id="#+id/node1"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="2"
android:id="#+id/node2"
android:layout_margin="20dp"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="5"
android:id="#+id/node5"
android:layout_margin="20dp"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="6"
android:id="#+id/node6"
android:layout_margin="20dp"
android:background="#drawable/node"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="3"
android:layout_margin="20dp"
android:id="#+id/node3"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="4"
android:id="#+id/node4"
android:layout_margin="20dp"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="7"
android:id="#+id/node7"
android:layout_margin="20dp"
android:background="#drawable/node"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:text="8"
android:id="#+id/node8"
android:layout_margin="20dp"
android:background="#drawable/node"/>
![enter image description here][1]</LinearLayout>
Edit - DrawView Class
How can I use this DrawView class to add the lines?
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View
{
Paint paint = new Paint();
static int startX, startY, endX, endY;
public DrawView(Context context,int startX, int startY, int endX, int endY)
{
super(context);
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
paint.setColor(Color.BLACK);
}
public DrawView(Context context)
{
super(context);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas)
{
canvas.drawLine(startX, startY, endX, endY,paint);
}
}
Edit 2:
This is not working for some reason:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.nodesImages);
DrawView drawView = new DrawView(getApplicationContext(),(int)node1.getX(),(int)node1.getY(),(int)node2.getX(),(int)node2.getY());
myLayout.addView(drawView);
Edit 3
There are so many libraries for creating complex graphs (line,bar,scatter,etc) . Why can't somebody create a library for tree graphs???? seems easy for those people!!!
based on your provide width and height of your button in layout. I use translation to do that. See the below code and the ugly Image for explain ##!
LinearLayout nodesImages = (LinearLayout) findViewById(R.id.nodesImages);
View v = new View(this); //this, because I write this code in the Activity class
//yah, try to understand values 20, 30,...
v.setLayoutParams(new ViewGroup.LayoutParams(dpToPx(20+20),dpToPx(2)));
v.setBackgroundColor(Color.BLACK);
nodesImages.addView(v);
v.setTranslationY(-dpToPx(30+20+20)-dpToPx(20+30/2));
v.setTranslationX(dpToPx(20+30));
with:
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
Ugly Image for explain:
UPDATE get location of view:
final LinearLayout nodesImages = (LinearLayout) findViewById(R.id.nodesImages);
//using addOnGlobalLayoutListener to make sure layout is happened.
nodesImages.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Remove the listener before proceeding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
nodesImages.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
nodesImages.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Button node1 = (Button)findViewById(R.id.node1);
int[] loc1 = new int[2];
node1.getLocationInWindow(loc1);//loc1[0] is x and loc1[1] is y
//for more information about this method, in Android Studio, just right-click -> Go To -> Declaration
Button node2 = (Button)findViewById(R.id.node2);
int[] loc2 = new int[2];
node2.getLocationInWindow(loc2);
View v = new View(getApplication());
v.setLayoutParams(new ViewGroup.LayoutParams(loc2[0]-loc1[0]-node1.getWidth(),dpToPx(2)));//dpToPx(20 + 20), dpToPx(2)));
v.setBackgroundColor(Color.BLACK);
nodesImages.addView(v);
v.setTranslationY(-dpToPx(30+20+20)-dpToPx(20+30/2));
v.setTranslationX(dpToPx(20+30));
}
}
);
Psst... Just do it in XML
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#android:color/black"/>
I've been searching for a while now to solve this problem. I created my own GIF view. I want to resize this GIF view such that it will cover the screen width, dependent on the screen size of the actual device. I got it working so that the GIF will show in it's actual size, but for the device of my friend, it is portrayed too small. I'm using the Movie class provided by Android.
I tried adjusting the canvas but that didn't work. I also tried multiple things with the onMeasure method but they also didn't work. The closest I got was that the GIF was larger but the window in which it appeared was still the same size as before. The code of the GIF view is down below, alongside with the xml file where it is used.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/spacevert"
tools:context="com.wina.hengst.hengstdrinkinggame.Multiplayer" >
<TextView
android:id="#+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="#string/be_ready"
android:textSize="50sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/linear"
android:layout_below="#id/timerTextView"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.wina.hengst.hengstdrinkinggame.GIF
android:id="#+id/gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear"
android:layout_centerHorizontal="true"
android:textSize="50sp"
android:gravity="center" />
<SeekBar
android:id="#+id/sliderMulti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<CheckBox
android:id="#+id/hiddenMulti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#id/sliderMulti"
android:text="#string/hidden"
android:textSize="25sp"
android:checked="true" />
<TextView
android:id="#+id/progress"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/sliderMulti"
android:textSize="25sp"
/>
<Button
android:id="#+id/startMulti"
android:text="#string/start"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/progress"
android:background="#color/Hengstgroen"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"/>
package com.wina.hengst.hengstdrinkinggame;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class GIF extends View {
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;
public GIF(Context context) {
super(context);
init(context);
}
public GIF(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GIF(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
setFocusable(true);
gifInputStream = context.getResources()
.openRawResource(R.drawable.sweetlemonade);
gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}
#Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}
public int getMovieWidth(){
return movieWidth;
}
public int getMovieHeight(){
return movieHeight;
}
public long getMovieDuration(){
return movieDuration;
}
#Override
protected void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (gifMovie != null) {
int dur = gifMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int)((now - mMovieStart) % dur);
gifMovie.setTime(relTime);
gifMovie.draw(canvas, 0, 0);
invalidate();
}
}
public void restart() {
mMovieStart = 0;
}
}
Your help would be much appreciated!
EDIT
I found it was too hard to try and solve this problem. I've just converted the gif into multiple images and used an animation xml provided by android. This is something I can resize easily by adjusting the xml with the ImageView. For the conversion I used GIMP and a plugin called export layers. If any of you know the answer, you can always write the answer here and I'll try it.
Try using Glide library, it can put GIFs in ImageViews. You also need to set layout_width in your ImageView to match_parent.
https://github.com/bumptech/glide
I have a edit text in which the user enters amount. What I want to do is set a textview value before it that is not editable by the user like "INR" and then the user will enter the amount in front of it. I want edittext to look like the below one. How can I do that?
<EditText
android:id="#+id/Eamnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
android:ems="10"
android:inputType="numberDecimal"
Try to use RelativeLayout which contains a TextView and an EditText. I did something below, try it.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:drawable/editbox_background" >
<TextView
android:id="#+id/constant_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:textColor="#C0C0C0"
android:text="INR"
android:background="#android:color/transparent" />
<EditText
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/constant_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#android:color/transparent"
android:paddingLeft="5dp"
android:text="100 000" />
</RelativeLayout>
You can use a RelativeLayout contaiing a TextView(for the hint) and an EditText for text input.
This code shows the example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="PasswordHint"
android:textColor="#aaaaaaaa"
android:textSize="30dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentLeft="true"
android:background="#00000000"
android:textSize="30dip" >
</EditText>
</RelativeLayout>
My suggestion is just use a background picture, which draws the text you want. Use the 9-patch to limit where user can input. This is not hint, but it can sloved your problem.
MyEditText.java
package com.example.myapplication24.UI;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import com.example.myapplication24.R;
public class MyEditText extends androidx.appcompat.widget.AppCompatEditText {
private String mhint = "";
private Paint mPaint;
private Context mContext;
private float paddingLeft = 0 ;
public MyEditText(Context context) {
super(context);
this.mContext = context;
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MyEditText);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
init();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyEditText,defStyleAttr,0);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
private void init() {
mPaint = new Paint();
mPaint.setTextSize(getTextSize());
mPaint.setColor(Color.GRAY);
paddingLeft = getPaddingLeft();
}
private int dp2px(float dp) {
return (int) (mContext.getResources().getDisplayMetrics().density * dp + 0.5f);
}
#Override
protected void onDraw(Canvas canvas) {
//Drawing mhint
canvas.drawText(mhint,dp2px(5),getHeight()*0.65f,mPaint);
super.onDraw(canvas);
}
}
layout.xml
<com.example.myapplication24.UI.MyEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mhint="Username: "
android:text="asd"
></com.example.myapplication24.UI.MyEditText>
attrs.xml
<!--my hint-->
<attr name="mhint" format="string"/>
<declare-styleable name="MyEditText">
<attr name="mhint"/>
</declare-styleable>
</resources>
Hope it can be helpful for latecomers...
demo