如何使画布适合屏幕

时间:2014-02-13 09:45:43

标签: java android canvas

我在我的应用程序中绘制画布,但问题是画布的大小与屏幕大小不同。大多数画布仅覆盖屏幕的右下部分。可以做些什么来使它适合屏幕。?

以下是它出现的屏幕截图:

enter image description here

这是我的代码:

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();
      }


    }
  }

这是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>

1 个答案:

答案 0 :(得分:0)

我认为Movie是自定义类?我无法看到Movie.draw()

中的内容
    int width = this.getWidth()/2; 
    int height = this.getHeight()/2; 

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

但是我可以看到你无缘无故地偏移图像。阿卡,开始在中心画画。图像本身不在此处居中的位置。

在绘制图像的绘制方法中,在绘制之前缩放图像。而是在原点画出来。

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

但是将视图的实际高度传递给draw方法:

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