First on the code
package org.example.guessdigit;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PuzzleView extends LinearLayout {
private int num; / / the number of digit you entered
private int diff;
int temp; / / the number you enter you entered
private Game game;
/ / Private float width; / / width of one tile
/ / Private float height; / / height of one tile
private int selX; / / X index of selection
private int selY; / / Y index of selection
/ / Private ArrayListtiles;
private Paint background;
private int imageHeight;
private int imageWidth;
private Canvas c = new Canvas ();
/ / Private LinearLayout layout;
/ / Private static final int ID = R.id.puzzleview;
LayoutParams params =
new LinearLayout.LayoutParams (
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
(Float) 1.0);
public PuzzleView (Context context) {
super (context);
/ / TODO Auto-generated constructor stub
this.game = (Game) context;
diff = game.getDiff ();
/ / Tiles = new ArrayList();
background = new Paint ();
background.setColor (getResources (). getColor (
R.color.puzzle_background));
/ / SetId (ID);
initTiles ();
}
public void setWH () {
final ImageView img = (ImageView) this.getChildAt (0);
ViewTreeObserver treeObsObj = img.getViewTreeObserver ();
treeObsObj.addOnPreDrawListener (
new ViewTreeObserver.OnPreDrawListener () {
@ Override
public boolean onPreDraw () {
/ / TODO Auto-generated method stub
imageWidth = img.getMeasuredWidth ();
imageHeight = img.getMeasuredHeight ();
return true;
}
});
}
protected void drawDigit (Canvas canvas, int digit) {
/ / TODO Auto-generated method stub
if (numImageView img = (ImageView) this.getChildAt (num);
int x = this.getChildAt (num) getLeft ();. / / Get the X coordinate ImageView results 0
int y = this.getChildAt (num) getTop ();. / / Get the Y coordinate ImageView results 0
int w = imageWidth;
int h = imageHeight;
Log.d ("puzzlve_width", this.getWidth () + ""); / / get the LinearLayout's width is 480
Log.d ("puzzlve_height", this.getHeight () + "") ;/ / the LinearLayout's Height is 63
System.out.println ("Image Height:" + imageHeight);
/ / Height = this.getHeight ();
/ / Width = this.getWidth ();
/ / Background.setTextSize (h * 0.75f);
/ / Background.setStyle (Style.FILL);
/ / Background.setTextScaleX (w / h);
/ / Background.setTextAlign (Paint.Align.CENTER);
canvas.drawText (digit + "", 20, 10, background); / / Why this method has no effect
drawTextnum + +;
}
}
@ Override
protected void onDraw (Canvas canvas) {
/ / TODO Auto-generated method stub
/ / DrawDigit (canvas, temp);
}
/ / Set the number you entered
public void addNumber (int i) {
if (numthis.removeViewAt (num);
this.addView (getImg1 (), num);
if (numthis.removeViewAt (num + 1);
this.addView (getImg2 (), num + 1);
/ / Temp = i;
drawDigit (c, i);
return;
/ / Num + +;
}
/ / Temp = i;
drawDigit (c, i);
}
}
/ / Img
after the electionpublic ImageView getImg2 () {
ImageView i = new ImageView (game);
i.setImageResource (R.drawable.login_input);
i.setLayoutParams (params);
return i;
}
/ Img
/ current selectionpublic ImageView getImg1 () {
ImageView i = new ImageView (game);
i.setImageResource (R.drawable.input_over);
i.setLayoutParams (params);
return i;
}
private void initTiles () {
this.addView (getImg2 ());
/ / Tiles.add (getImg2 ());
this.setLayoutParams (new LayoutParams (params));
this.setOrientation (LinearLayout.HORIZONTAL);
for (int i = 1; i/ / Tiles.add (getImg1 ());
this.addView (getImg1 ());
}
setWH ();
}
@ Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
/ / TODO Auto-generated method stub
super.onSizeChanged (w, h, oldw, oldh);
}
}
I guess the problem has two, one is the coordinates and size of the ImageView in LinearLayout class access are 0
Another method is the Canvas drawText no effect, my LinearLayout's width is 480, height is 63
However drawText (s, 20, 10, background); method has no effect
I wanted to call based on the location ImageView drawText, unfortunately coordinates are 0
Later, I used the specified coordinates (20,10) have no effect, does not display the string
Seeking heroes! ! ! Thank you first
Reply:
No people? Sofa he sat
Reply:
Reply:
No people? Seeking help
Reply:
Do not sink ah! Seeking to explain why there is no effect drawtext methods
Reply:
See the background color you use to drawtext, the color will not be a problem ah
Reply:
Reply:
not color problems
Reply:
Look at your code drawDigit not called in onDraw, ah, you have to call in the job onDraw
Reply:
I have encountered this problem. drawText, drawBitmap .. all the painting had no effect.
@ Override
protected void onDraw (Canvas canvas) {
/ / TODO Auto-generated method stub
super.onDraw (canvas);
System.out.println ("BublleTextView> onDraw ----------------------");
Drawable drawable = this.getResources (). GetDrawable (
R.drawable.ic_launcher);
Bitmap mBitmap = ((BitmapDrawable) drawable) getBitmap ();.
canvas.drawBitmap (mBitmap, 0, 0, null) ;/ / start drawing in 0,0 coordinates into the original image src
/ / Draw the progress
int width = getWidth ();
int height = getHeight ();
mPaint.setTypeface (Typeface.defaultFromStyle (Typeface.BOLD));
mPaint.setColor (Color.BLACK);
mPaint.setTextSize (30);
canvas.drawText ("ddddddddddddddddddddddddddddddddddddddddd", 1, 1,
mPaint);
mPaint.setColor (Color.BLACK);
canvas.drawText (String.valueOf ("aaaaaaaaaaaaaa"), width / 2,
height / 2, mPaint);
canvas.drawPoint (11,3, mPaint);
}
I modified the Launcher4.03, drawing on the icon above the text, drawing pictures, but no effect. Great God seeking some research.
No comments:
Post a Comment