I have a GIF(using Movie and CustomView), which I am not able to align center of my parent RelativeLayout, with following code:
public class GIFView extends View {
private Movie mMovie;
private long mMovieStart;
int mGIFDrawable = R.raw.butterfly;
public GIFView(Context context) {
super(context);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(mGIFDrawable);
mMovie = Movie.decodeStream(is);
}
public GIFView(Context context, AttributeSet attrSet) {
super(context, attrSet);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(mGIFDrawable);
mMovie = Movie.decodeStream(is);
}
public GIFView(Context context, AttributeSet attrSet, int defStyle) {
super(context, attrSet, defStyle);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(mGIFDrawable);
mMovie = Movie.decodeStream(is);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
super.onDraw(canvas);
final long now = SystemClock.uptimeMillis();
if (mMovieStart == 0) {
mMovieStart = now;
}
final int relTime = (int) ((now - mMovieStart) % mMovie.duration());
mMovie.setTime(relTime);
mMovie.draw(canvas, 10, 10);
this.invalidate();
}
}
I am not able to align this view center of my parent RelativeLayout.
XML declaration is as follows:
<com.example.common.GIFView
android:id="#+id/gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
Related
I'm trying to draw a custom text on an ImageButton. I have overwritten the method onDraw(Canvas) but it didn't work. This is the code:
public class CustomImageButton extends ImageButton {
private static final String TAG = "CustomImageButton";
private String text = "";
private float textSize;
private int textColor;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CustomImageButton(Context context) {
super(context);
initialize(null);
}
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(attrs);
}
public CustomImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(attrs);
}
private void initialize(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomImageButton);
text = a.getString(R.styleable.CustomImageButton_text);
textSize = a.getDimension(R.styleable.CustomImageButton_textSize, 20);
textColor = a.getColor(R.styleable.CustomImageButton_textColor, getResources().getColor(R.color.white));
//setBackgroundColor(getResources().getColor(R.color.transparent));
a.recycle();
invalidate();
}
}
#Override
protected void onDraw(Canvas canvas) {
Typeface font = Typefaces.get(getContext(), "fonts/my-font.ttf");
paint.setColor(Color.WHITE);
paint.setTypeface(font);
paint.setTextSize(100);
paint.setTextAlign(Paint.Align.CENTER);
int xPos = canvas.getWidth() / 2;
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
canvas.drawText(text, xPos, yPos, paint);
}
}
And XML Layout:
<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:background="#OOOOOO"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.my.app.CustomImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:text="Hello!"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
. . .
</RelativeLayout>
I used setWillNotDraw(false) but it seems that does not work. I have verified that my custom view has a few correct dimensions.
I have an application include gif file that extended from a view class. I want to display gif file fill parent means fit on the screen, but my code show it in real size.
This is my code for gif view that use from the Movie:
public class GifView extends View{
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long movieStart;
public GifView(Context context) {
super(context);
init(context);
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GifView(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.m);
gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}
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;
}
protected void onDraw(Canvas canvas) {
long now = SystemClock.uptimeMillis();
if(movieStart == 0){
movieStart = now;
}
if(gifMovie != null){
int dur = gifMovie.duration();
if(dur == 0){
dur = 1000;
}
int relTime = (int)((now - movieStart)%dur);
gifMovie.setTime(relTime);
gifMovie.draw(canvas, 0, 0);
invalidate();
}
}
}
this is xml's code:
<com.example.test.GifView
android:id="#+id/gefview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I search lots and test this code:
public class SampleView extends View {
private Movie mMovie;
private long mMovieStart;
public SampleView(Context context) {
super(context);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.m);
mMovie = Movie.decodeStream(is);
}
public SampleView(Context context, AttributeSet attrSet) {
super(context, attrSet);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.m);
mMovie = Movie.decodeStream(is);
}
public SampleView(Context context, AttributeSet attrSet, int defStyle) {
super(context, attrSet, defStyle);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.mojri);
mMovie = Movie.decodeStream(is);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0x00000000);
Paint p = new Paint();
p.setAntiAlias(true);
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
getHeight() / 2 - mMovie.height() / 2);
invalidate();
}
}
}
and this:
public class Gif_loading_View extends WebView {
public Gif_loading_View(Context context) {
super(context);
// TODO Auto-generated constructor stub
loadUrl("myUrl/m.gif");
}
}
But these views show real size too, and i can't change the size of gif file.
Please help me.
I have CircleView custom object, that works fine.
I want to compound it with a TextView below, like this:
So I made CircleCheckBoxWithText that compound the two views,
The problem is that if the first view that I'm adding is my custom view, and the second view is the TextView, I can't see the TextView.
(When the first view that I'm adding is the TextView and the second is my custom view, I can see both views)
CircleView:
public class CircleView extends View
{
private float mDimension;
private int mFillColor;
private int mBorderColor;
private boolean mIsChecked;
private Paint mFillPaint = new Paint();
private Paint mStrokePaint = new Paint();
public CircleView(Context context)
{
super(context);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public CircleView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Get the styledAttributes from the xml
TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0);
try
{
mDimension = styledAttributes.getDimensionPixelOffset(R.styleable.CircleView_dim, 20);
mFillColor = styledAttributes.getColor(R.styleable.CircleView_fill_color, Color.WHITE);
mBorderColor = styledAttributes.getColor(R.styleable.CircleView_border_color, Color.WHITE);
mIsChecked = styledAttributes.getBoolean(R.styleable.CircleView_checked, false);
}
finally
{
styledAttributes.recycle();
}
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// init canvas
canvas.drawARGB(0, 0, 0, 0);
// Set the Fill
mFillPaint.setAntiAlias(true);
mFillPaint.setDither(true);
mFillPaint.setColor(mFillColor);
// Set the Stroke
mStrokePaint.setAntiAlias(true);
mStrokePaint.setDither(true);
mStrokePaint.setColor(mBorderColor);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setStrokeWidth(8f);
canvas.drawCircle(mDimension / 2, mDimension / 2, mDimension / 2 - 0.2f, mFillPaint);
if(mIsChecked)
{
canvas.drawCircle(mDimension / 2, mDimension / 2, mDimension / 2 - mStrokePaint.getStrokeWidth() / 2 + 0.1f, mStrokePaint);
}
}
}
CircleCheckBoxWithText:
public class CircleCheckBoxWithText extends LinearLayout
{
public CircleCheckBoxWithText(Context context)
{
super(context);
setOrientation(VERTICAL);
}
public CircleCheckBoxWithText(Context context, AttributeSet attrs)
{
super(context, attrs);
setOrientation(VERTICAL);
addView(new CircleView(context, attrs));
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setText("TEST");
addView(textView);
}
public CircleCheckBoxWithText(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
setOrientation(VERTICAL);
}
}
I need to draw an empty circle with a margin of 10 px. The problem that i've encountered is that i need to simulate the draw of the circle in 2 seconds and after that to start drawing on the top of it another one with another color. I'm using a custom view and i've tried to implement my logic into onDraw method and invalidate the view every 50 milisecond. The problem is that i can't manage to draw the circle...i draw only crapy figures. Does somebody know how can i draw a circle without using the canvas.drawCircle method because that method is drawing the circle directly without animation.
My current code
public class CustomAnimationView extends View{
private Canvas canvas;
private int count = 0;
private Paint paint;
private int mLeft;
private int mRight;
private int mBottom;
private int mTop;
public CustomAnimationView(Context context) {
super(context);
}
public CustomAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttributes(attrs);
}
private void setAttributes(AttributeSet attrs) {
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
if(paint == null){
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
}
if(count<150){
drawFirstQuarter(count);
}
count++;
}
public void drawFirstQuarter(int count){
RectF oval = new RectF(mLeft, mTop, mRight, mBottom);
canvas.drawArc(oval, 90, 30, true, paint);
}
public void setRect(int top, int bottom, int left, int right){
mBottom = bottom;
mTop = top;
mLeft = left;
mRight = right;
}
}
Right now I'm just tring to draw a bit of a circle.
Thanks. I've solved it.
Here is a code sample
public class CustomAnimationView extends View{
private Canvas canvas;
private int mCount = 0;
private Paint paint1;
private Paint paint2;
private RectF oval1;
private Context context;
private int mColorCount = 0;
public CustomAnimationView(Context context) {
super(context);
this.context = context;
}
public CustomAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setAttributes(attrs);
}
private void setAttributes(AttributeSet attrs) {
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
if(paint1 == null){
paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setStyle(Style.STROKE);
paint1.setStrokeWidth(10);
}
if(paint2 == null){
paint2 = new Paint();
paint2.setAntiAlias(true);
paint2.setStyle(Style.STROKE);
paint2.setStrokeWidth(10);
}
if(mCount % 360 == 0 ){
mColorCount++;
}
if(mColorCount % 2 == 0){
paint1.setColor(context.getResources().getColor(R.color.white));
paint2.setColor(context.getResources().getColor(R.color.black));
}else{
paint2.setColor(context.getResources().getColor(R.color.white));
paint1.setColor(context.getResources().getColor(R.color.black));
}
if(oval1 == null)
oval1 = new RectF(5,5,canvas.getWidth()-5, canvas.getHeight()-5);
drawFirstQuarter(mCount, oval1);
}
public void drawFirstQuarter(int count, RectF oval){
canvas.drawArc(oval, 90, 360, false, paint2);
canvas.drawArc(oval, 90, count, false, paint1);
if(mCount == 330)
mCount = 0;
else
mCount += 30;
}
}
I want to have a line between each row in TextView.
Can original TextView do this?
If not, how can I do it?
ANSWER:
Thanks to #Slartibartfast reference and advice. I made a customized TextView. And I get something like this.
This is what I want!
The code:
public class LinedTextView extends TextView {
private Rect mRect;
private Paint mPaint;
public LinedTextView(Context context) {
super(context);
initialize();
}
public LinedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public LinedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000ff);
}
#Override
protected void onDraw(Canvas canvas) {
int cnt = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
for (int i = 0; i < cnt; i++) {
int baseLine = getLineBounds(i, r);
canvas.drawLine(r.left, baseLine + 1, r.right, baseLine + 1, paint);
}
super.onDraw(canvas);
}
}
Use the following line of code below your TextView
<View android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#android:color/background_dark" />
You can configure it according to your need.
You can also use ListView with divider.