2017-09-28 3 views
2

Ich habe versucht, einige Sprite-Animation in Android zu tun (es ist eine College-Übung), aber ich habe ein seltsames Problem. Anstatt das Rechteck zu zeichnen, das ich als Quelle definiert habe, zeichnet es genau die Hälfte dieses Rechtecks.Canvas.drawBitmap Schneidequelle in der Hälfte

Hier ist der Code für meine Ziehverfahren

public class Sprite { 
Bitmap image; 
Point frameSize; 
int[] rows; 
public int curFrame; 
public int curRow; 

public void draw(Canvas c, Paint p, Vector2 pos) 
{ 
    Rect src; 
    int left = curFrame * frameSize.x; 
    int top = curRow * frameSize.y; 
    int right = left + frameSize.x; 
    int bottom = top + frameSize.y; 

    src = new Rect(left, top, right, bottom); 
    Rect dest = new Rect((int)pos.x, 0, (int)pos.x + frameSize.x, frameSize.y); 

    c.drawBitmap(image, src, dest, p); 
} 

Hier ist das Bild, das ich bin mit Megaman SpriteSheet

Alle Rahmen sind gleich groß (44x40), und das Bild ist 440x80, und ich gebe diese Wert als frameSize, wenn ich die Methode aufrufen.

Punkt ist im Grunde ein Objekt, das ein Paar x, y enthält.

Vector2 ist ein Objekt, das ein Paar x, y Floats enthält.

Ich render auch etwas Text für Debug-Zwecke.

Hier sind ein paar Screenshots: screenshot: frame zeroscreenshot: frame onescreenshot: frame two

Es tut mir leid, die Screenshots sind enorm. Ich weiß nicht, ob es einen Weg gibt, in Stackoverflow sie kleiner zu zeigen

Antwort

-1

enter image description here

ich Ihren Code nicht sehen können. Wichtig ist dies:

int left = currIndex * peopleWidth; 
int top = 40; 
int right = left + peopleWidth; 
int bottom = 80; 
//clip the bitmap to show 
Rect src = new Rect(left, top, right, bottom); 
//set the display location in view 
Rect display = new Rect(0, 0, mWidth, mHeight); 
canvas.drawBitmap(mBitmap, src, display, null); 

Hier ist mein Code:

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.support.annotation.Nullable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 

import com.rajesh.customcamera.R; 

import java.util.concurrent.TimeUnit; 

import io.reactivex.Observable; 
import io.reactivex.android.schedulers.AndroidSchedulers; 
import io.reactivex.annotations.NonNull; 
import io.reactivex.functions.Consumer; 

/** 
* Created by rajesh.zhu on 2017/9/28. 
*/ 

public class MovingPeopleView extends View { 
    private static final String TAG = "MovingPeopleView"; 
    private int defaultWidth = 44; 
    private int defaultHeight = 40; 
    private int mWidth = 0; 
    private int mHeight = 0; 
    private int peopleWidth = 44; 
    private int peopleHeight = 40; 
    private int currIndex = 0; 

    private Bitmap mBitmap = null; 
    private boolean isRunning = false; 

    public MovingPeopleView(Context context) { 
     this(context, null); 
    } 

    public MovingPeopleView(Context context, @Nullable AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public MovingPeopleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    @Override 
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     int widthSize = MeasureSpec.getSize(widthMeasureSpec); 

     int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     int heightSize = MeasureSpec.getSize(heightMeasureSpec); 

     if (widthMode == MeasureSpec.EXACTLY) { 
      //match_parent || absolute_value 
      mWidth = widthSize; 
     } else if (widthMode == MeasureSpec.AT_MOST) { 
      //wrap_content 
      mWidth = Math.min(defaultWidth, widthSize); 
     } else { 
      mWidth = defaultWidth; 
     } 

     if (heightMode == MeasureSpec.EXACTLY) { 
      mHeight = heightSize; 
     } else if (heightMode == MeasureSpec.AT_MOST) { 
      mHeight = Math.min(defaultHeight, heightSize); 
     } else { 
      mHeight = defaultHeight; 
     } 

     setMeasuredDimension(mWidth, mHeight); 
    } 

    private void init(Context context) { 
     mBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_moving_people); 
     //Log.i(TAG, "Width:" + mBitmap.getWidth() + ", Height:" + mBitmap.getHeight()); 
     //you can set the peopleWidth and peopleHeight here 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     super.draw(canvas); 
     int left = currIndex * peopleWidth; 
     int top = 40; 
     int right = left + peopleWidth; 
     int bottom = 80; 
     //clip in bitmap to show 
     Rect src = new Rect(left, top, right, bottom); 
     //set the show location in view 
     Rect display = new Rect(0, 0, mWidth, mHeight); 
     canvas.drawBitmap(mBitmap, src, display, null); 
    } 

    public void start() { 
     isRunning = true; 
     run(); 
    } 

    public void stop() { 
     isRunning = false; 
    } 

    private void run() { 
     if (isRunning) { 
      Observable 
        .timer(100, TimeUnit.MILLISECONDS) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(new Consumer<Long>() { 
         @Override 
         public void accept(@NonNull Long aLong) throws Exception { 
          currIndex++; 
          if (currIndex == 10) { 
           currIndex = 0; 
          } 
          Log.i(TAG, "run"); 
          invalidate(); 
          run(); 
         } 
        }); 
     } 
    } 

    public void recycler() { 
     if (mBitmap != null) { 
      mBitmap.recycle(); 
      mBitmap = null; 
     } 
    } 
} 

Sie es in XML verwenden können:

<com.rajesh.customcamera.view.MovingPeopleView 
    android:id="@+id/moving_people" 
    android:layout_width="50dp" 
    android:layout_height="50dp" /> 
+0

beobachtbare Teil gefährlich aussieht. Sie sollten den Code woanders ablegen und auf das Abonnement achten. Vielleicht starten Sie in OnAttach eine Abmeldung in OnDetach oder auf Sichtbarkeitswechsel. –

Verwandte Themen