fil*_*pst 1 android bitmap matrix imageview
我正在开发一个应用程序,用户可以在屏幕上移动他的图像,然后保存它.问题是在活动开始时将Bitmap定位在ImageView中.
这是XML:
<RelativeLayout
android:id="@+id/image_content_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/top_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="matrix"
android:gravity="center|center_vertical"
android:layout_gravity="center|center_vertical"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我正在使用onTouch移动ImageView(好吧,不是ImageView,但它的矩阵),它运行良好.
@Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
mode = DRAG;
start.set((int) event.getX(), (int) event.getY());
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
break;
}
view.setImageMatrix(matrix);
return true;
}
Run Code Online (Sandbox Code Playgroud)
问题是Bitmap在Activity开始时的位置.它在Top | Left而不是Center上对齐.像这样:
任何人都可以帮我把它集中在ImageView的开头?
如果要将位图对齐到中心,则ImageView布局应为:
<ImageView
android:id="@+id/top_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"/>
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您需要scaleType"matrix",请使用下一个解决方案:
<ImageView
android:id="@+id/top_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
/>
Run Code Online (Sandbox Code Playgroud)
然后在代码更改图像的偏移量:
final ImageView imageView = (ImageView) findViewById(R.id.top_image);
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= 16) {
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Drawable drawable = imageView.getDrawable();
Rect rectDrawable = drawable.getBounds();
float leftOffset = (imageView.getMeasuredWidth() - rectDrawable.width()) / 2f;
float topOffset = (imageView.getMeasuredHeight() - rectDrawable.height()) / 2f;
Matrix matrix = imageView.getImageMatrix();
matrix.postTranslate(leftOffset, topOffset);
imageView.setImageMatrix(matrix);
imageView.invalidate();
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4404 次 |
最近记录: |