以下是我编写的代码片段,试图理解Matrix(postRotate
和postTranslate
更为准确)。
活动:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mImageView2;
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.btn:
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postTranslate(200, 0);
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
Bitmap bitmap2 = Bitmap.createBitmap(
bitmap,
0,
0,
bitmap.getWidth(),
bitmap.getHeight(),
matrix,
true
);
mImageView2.setImageBitmap(bitmap2);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(this);
mImageView2 = (ImageView) findViewById(R.id.image2);
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="sample2.com.sample_app.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:scaleType="matrix"
tools:src="@mipmap/ic_launcher" />
</LinearLayout>
我做错了什么?