我试图从一个spritesheet裁剪精灵,但是裁剪的图片是不是我指定的图像的一部分。安卓:从位图裁剪位图,并设置为ImageView的SRC
spritesheet包含大小为32 x 32像素的精灵,因此第一个精灵应该在像素0到32(水平)和0到32(垂直),假设最左上角的像素是[0,0 ]。
Bitmap spritesheet = BitmapFactory.decodeResource(getResources(), R.drawable.spritesheet_32x32);
Bitmap sprite = Bitmap.createBitmap(spritesheet, 0, 0, 32, 32);
ImageView只是一个普通的ImageView,没有设置src,width或height。
ImageView view = (ImageView)findViewById(R.id.player1);
view.setImageBitmap(sprite);
但显示的图像只是第一个精灵的一小部分。 我怎么监督?是view.setImageBitmap(sprite);
这个正确的方法?是否需要ImageView的设置位图前修复宽度和高度?
编辑:也许我应该对我的布局更加清晰。 LinearLayout上排列着两个ImageView。这里的XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="-1"
android:id="@+id/player1"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/player2"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
http://stackoverflow.com/questions/18232034/how-can-i-crop-a-bitmap-for-imageview –