作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试在 Android 中制作一些 Sprite 动画(这是一项大学练习),但我遇到了一个奇怪的问题。它没有绘制我定义为源的矩形,而是恰好绘制了该矩形的一半。
这是我的绘图方法的代码
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);
}
所有帧的大小相同 (44x40),图像为 440x80,我在调用该方法时将此值指定为 frameSize。
Point 基本上是一个包含一对 x,y 整数的对象。
Vector2 是一个包含一对 x,y float 的对象。
为了调试目的,我还渲染了一些文本。
对不起,截图太大了。我不知道是否有办法在 StackOverflow 中将它们显示得更小
最佳答案
我看不到你的代码。这有什么重要的:
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);
这是我的代码:
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;
}
}
}
您可以在 XML 中使用它:
<com.rajesh.customcamera.view.MovingPeopleView
android:id="@+id/moving_people"
android:layout_width="50dp"
android:layout_height="50dp" />
关于java - Canvas.drawBitmap 将源代码切成两半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46459159/
我有一堆大的 GeoTiff 文件(1.4GB,4 个纬度 x 8 个经度)。 我需要将每个瓷砖切成 1 度纬度 x 1 度长的瓷砖(每个瓷砖都只有一点点超龄)。 这是 GeoTiff 文件之一的 g
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!