常使用的类Image
2021-06-27 本文已影响0人
大旺旺的弟弟小旺旺
最长使用的类Image,游戏中的显示,大多数都使用到了image.使用方法:
Image image = new Image(new Texture("1.png"));
stage.addActor(image);
两行代码就可以显示出图片,下来就是对位置,透明度等的设置。
- 构造函数
public Image(Drawable drawable, Scaling scaling, int align) {
setDrawable(drawable);
this.scaling = scaling;
this.align = align;
setSize(getPrefWidth(), getPrefHeight());
}
然后就是布局,然后绘制,至于其他什么缩放,位置的都是Actor中的部分,先不说。
布局
public void layout() {
if (drawable == null) return;
float regionWidth = drawable.getMinWidth();
float regionHeight = drawable.getMinHeight();
float width = getWidth();
float height = getHeight();
Vector2 size = scaling.apply(regionWidth, regionHeight, width, height);
imageWidth = size.x;
imageHeight = size.y;
if ((align & Align.left) != 0)
imageX = 0;
else if ((align & Align.right) != 0)
imageX = (int) (width - imageWidth);
else
imageX = (int) (width / 2 - imageWidth / 2);
if ((align & Align.top) != 0)
imageY = (int) (height - imageHeight);
else if ((align & Align.bottom) != 0)
imageY = 0;
else
imageY = (int) (height / 2 - imageHeight / 2);
}
根据设置的中心,来得到imageX和imageY.
绘制
public void draw(Batch batch, float parentAlpha) {
validate();
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
float x = getX();
float y = getY();
float scaleX = getScaleX();
float scaleY = getScaleY();
try{
if (drawable instanceof TransformDrawable) {
float rotation = getRotation();
if (scaleX != 1 || scaleY != 1 || rotation != 0 || getOriginX() != 0 || getOriginY() != 0) {
((TransformDrawable) drawable).draw(batch, x + imageX, y + imageY, getOriginX() - imageX, getOriginY() - imageY,
imageWidth, imageHeight, scaleX, scaleY, rotation);
return;
}
}
if (drawable != null) {
drawable.draw(batch, x + imageX, y + imageY, imageWidth * scaleX, imageHeight * scaleY);
}
}catch (Exception e){
System.out.println(toString());
}
}
先布局,然后 进行绘制。绘制的主体是drawable。
drawble
drawable是什么东西??
public interface Drawable
是个接口,好吧,那换一个看,TextureRegionDrawable.这个的绘制都是region.
public void draw (Batch batch, float x, float y, float width, float height) {
batch.draw(region, x, y, width, height);
}
public void draw (Batch batch, float x, float y, float originX, float originY, float width, float height, float scaleX,
float scaleY, float rotation) {
batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
}
绘制的region又是什么东西??
region它是一个TextureRegion,定义纹理的矩形区域。所用坐标系的原点位于左上角,x 轴 * 指向右侧,y 轴指向下方..
/** Constructs a region the size of the specified texture. */
public TextureRegion (Texture texture) {
if (texture == null) throw new IllegalArgumentException("texture cannot be null.");
this.texture = texture;
setRegion(0, 0, texture.getWidth(), texture.getHeight());
}
/** @param width The width of the texture region. May be negative to flip the sprite when drawn.
* @param height The height of the texture region. May be negative to flip the sprite when drawn. */
public TextureRegion (Texture texture, int width, int height) {
this.texture = texture;
setRegion(0, 0, width, height);
}
TextureRegion举个简单例子,
一张图片只想显示10,10到20,20位置的大小,可以理解为将一个大图裁剪为了一张小图。用裁剪出来的这一部分进行展示,常用的地方是做动画的时候或者是读取一个图集上面一个小图的时候,指定坐标的位置将这一部分拿出来使用。简单理解,他是一个剪刀,我们需要告诉它,从哪里开始剪它。