我使用Matrix和Bitmap for imageView, 我想设置位置和图像缩放为imageView。 (imageView总是显示x = 0,y = 0) Scalling工作,但翻译位置不起作用。 请帮我解决这个问题。 这是我的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_entrance, container);
ImageButton setting_button = (ImageButton) root.findViewById(R.id.settings_next);
setting_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("next_page", false);
startActivity(intent);
}
});
final ImageView backGroundImageView = (ImageView) root.findViewById(R.id.fragment_entrance_background_imageview);
mLogoImageView = (ImageView) root.findViewById(R.id.fragment_entrance_logo_imageview);
Matrix mMatrix = new Matrix();
mMatrix.reset();
mMatrix.postTranslate(mCenterX - mImgWidth*0.5f, mCenterY - mImgHeight * 0.5f);
mMatrix.postScale(mScaleX, mScaleY, mCenterX, mCenterY);
mLogoImageView.invalidate();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mLogoImgId);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) mImgWidth, (int) mImgHeight, mMatrix, true);
mLogoImageView.setImageBitmap(newBitmap);
return root;
}
and here is xml file.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/entrance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.receptionist.receptionist.Entrance.EntranceFragment">
<ImageView
android:id="@+id/fragment_entrance_background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="0dp">
<ImageView
android:id="@+id/fragment_entrance_logo_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"/>
</RelativeLayout>
<ImageView
android:id="@+id/dummy_button"
android:layout_width="111dp"
android:layout_height="94dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="40dp"
android:background="@color/black_overlay"
android:src="@drawable/button_next" />
<ImageButton
android:id="@+id/settings_next"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="right|top"
android:background="@color/black_overlay"
android:src="@drawable/setting_w" />
</FrameLayout>