简体   繁体   中英

How to make the canvas fit the screen

I am drawing a canvas in my app but the problem is that the canvas is not of the same size what the screen is. Mostly the canvas is covering only bottom right part of the screen. Whant can be done to make it fit to the screen.?

Here is the screen shot of how its appearing:

在此处输入图片说明

Here is my code:

public class AnimationView extends View 
{

    private Movie mMovie;
    private long mMovieStart;
    private static final boolean DECODE_STREAM = true;
    private static byte[] streamToBytes(InputStream is) {
      ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
      byte[] buffer = new byte[1024];
      int len;
      try {
        while ((len = is.read(buffer)) >= 0) {
          os.write(buffer, 0, len);
        }
      } catch (java.io.IOException e) {
      }
      return os.toByteArray();
    }

     public AnimationView(Context context,AttributeSet attrs) 
     {

      super(context,attrs);
      setFocusable(true);
      java.io.InputStream is;
      is = context.getResources().openRawResource(R.drawable.flag);
      if (DECODE_STREAM) {
        mMovie = Movie.decodeStream(is);
      } else {
        byte[] array = streamToBytes(is);
        mMovie = Movie.decodeByteArray(array, 0, array.length);
      }
    }
    @Override
    public void onDraw(Canvas canvas) {
     long now = android.os.SystemClock.uptimeMillis();
      if (mMovieStart == 0) { // first time
        mMovieStart = now;
      }
      if (mMovie != null) {
        int dur = mMovie.duration();
        if (dur == 0) {
          dur = 3000;
        }
        int relTime = (int) ((now - mMovieStart) % dur);
       Log.d("", "real time :: " +relTime);
        mMovie.setTime(relTime);

        int width = this.getWidth()/2; 
        int height = this.getHeight()/2; 

        mMovie.draw(canvas, width, height);
        invalidate();
      }


    }
  }

Here is XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#074068"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <com.androidqa.AnimationView
        android:id="@+id/View"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:visibility="gone"
        android:background="#074068" />

</LinearLayout>

I assume Movie is a custom class? I cant see what is in Movie.draw()

    int width = this.getWidth()/2; 
    int height = this.getHeight()/2; 

    mMovie.draw(canvas, width, height); // this method

But I can see you are offsetting the image for no reason. Aka, starting to draw at the center. Where the image itself is not centered at this point.

Inside your draw method, where you draw the image, scale the image before drawing. And draw this instead, at the origion.

bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
canvas.drawBitmap(bitmap, 0, 0, null);

But pass in the actual height of the view to your draw method:

    int width = this.getWidth(); 
    int height = this.getHeight(); 
    mMovie.draw(canvas, width, height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM