我正在使用https://github.com/chrisbanes/PhotoView在我的自定义图像视图上启用捏缩放功能,但是,在切换按钮上实现该功能时遇到问题。当用户单击切换按钮 On 时,我想使图像可缩放。缩放后,当用户单击缩放按钮关闭时,图像应仍处于其缩放位置但不可缩放(因为我有另一个 onTouchListener 用户可以单击并检测图像的颜色)。问题是,当切换按钮改变状态(打开到关闭)时,缩放将重置。请帮助我,我很抱歉我的糟糕解释。谢谢你。
以下是我的部分代码:
case R.id.btnZoom:
PhotoViewAttacher photoView= new PhotoViewAttacher(ivImageEditor);
photoView.update();
if(btnZoom.isChecked()){
//photoView.setZoomable(true);
}else if (!btnZoom.isChecked()){
photoView.setZoomable(false);
}
break;
更新:下面是我为 ImageView 放置 onTouch 事件以获取像素颜色的活动。(而 PhotoView 库也覆盖 onTouch 以启用图像缩放)。我试图从这个问题中实现该方法:Android PhotoView Keep Zoom After Orientation Change to keep scale size/ the zoom size on different state of toggle button 但我不明白是什么mScreenBase
。我希望你能理解我的问题是什么。谢谢。
ImageEditorActivity.java:
package com.example.satellite.coloridentifier;
public class ImageEditorActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener {
public ImageView ivImageEditor, ivColorPicked;
public TextView tvColorName, tvHexCode, tvRGB;
public ToggleButton btnZoom;
public Bitmap myBitmap;
private float ZoomLevel;
private Matrix theMatrix;
// private RectF rectF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_editor);
btnZoom = (ToggleButton) findViewById(R.id.btnZoom);
btnZoom.setOnClickListener(this);
ivImageEditor = (ImageView) findViewById(R.id.ivImageEditor);
ivColorPicked = (ImageView) findViewById(R.id.ivColorPicked);
tvColorName = (TextView) findViewById(R.id.tvColorName);
tvHexCode = (TextView) findViewById(R.id.tvHexCode);
tvRGB = (TextView) findViewById(R.id.tvRGB);
// get image or get photo if exist
try {
String imagePath = getIntent().getExtras().getString("imagepath");
String photo = getIntent().getExtras().getString("photoUri");
if (imagePath != null) {
myBitmap = BitmapFactory.decodeFile(imagePath);
ivImageEditor.setImageBitmap(myBitmap);
} else if (photo != null) {
Uri photoUri = Uri.parse(photo);
myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
ivImageEditor.setImageBitmap(myBitmap);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "No image or photo", Toast.LENGTH_SHORT).show();
}
//get pixel colors from image on touched parts
ivImageEditor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!(ivImageEditor.getDrawable() == null)) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
ivImageEditor.buildDrawingCache();
Bitmap bitmap = ivImageEditor.getDrawingCache();
if (touchX > 0 && touchY > 0 && touchX < bitmap.getWidth() && touchY < bitmap.getHeight()) {
int pixel = bitmap.getPixel(touchX, touchY);
int red = Color.red(pixel);
int myRed = (int) Math.round(red * 100.0 / 255);
int green = Color.green(pixel);
int myGreen = (int) Math.round(green * 100.0 / 255);
int blue = Color.blue(pixel);
int myBlue = (int) Math.round(blue * 100.0 / 255);
updateColorRGB(red, green, blue);
updateColorPercentage(myRed, myGreen, myBlue);
}
}
return true;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnZoom:
final PhotoViewAttacher photoView = new PhotoViewAttacher(ivImageEditor);
if (btnZoom.isChecked()) {
photoView.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
@Override
public void onMatrixChanged(RectF rectF) {
theMatrix = photoView.getDisplayMatrix();
float[] theFloat = new float[9];
theMatrix.getValues( theFloat );
RectF theRect = photoView.getDisplayRect();
if (theRect != null)
{
if( theRect.left > ( ivImageEditor.getWidth() / 2 ) || ( theRect.left >= 0 ) )
{
theRect.left = 0;
}
else
{
theRect.left = ( theRect.left - ( ivImageEditor.getWidth() / 2 ) ) / mScreenBase;
}
if( theRect.top > ( ivImageEditor.getHeight() / 2 ) || ( theRect.top >= 0 ) )
{
theRect.top = 0;
}
else
{
theRect.top = ( theRect.top - ( ivImageEditor.getHeight() / 2 ) ) / mScreenBase;
}
ZoomLevel = photoView.getScale();
}
}
});
} else if (!btnZoom.isChecked()) {
photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
photoView.setDisplayMatrix(theMatrix);
photoView.setScale(ZoomLevel);
// Toast.makeText(this, "level"+ZoomLevel, Toast.LENGTH_SHORT).show();
ivImageEditor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!(ivImageEditor.getDrawable() == null)) {
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
ivImageEditor.buildDrawingCache();
Bitmap bitmap = ivImageEditor.getDrawingCache();
if (touchX > 0 && touchY > 0 && touchX < bitmap.getWidth() && touchY < bitmap.getHeight()) {
int pixel = bitmap.getPixel(touchX, touchY);
int red = Color.red(pixel);
int myRed = (int) Math.round(red * 100.0 / 255);
int green = Color.green(pixel);
int myGreen = (int) Math.round(green * 100.0 / 255);
int blue = Color.blue(pixel);
int myBlue = (int) Math.round(blue * 100.0 / 255);
updateColorRGB(red, green, blue);
updateColorPercentage(myRed, myGreen, myBlue);
}
}
return true;
}
});
}
break;
default:
break;
}
}
public void updateColorPercentage(int redColor, int greenColor, int blueColor) {
DbHelper myDbHelper = new DbHelper(ImageEditorActivity.this);
String colorName = myDbHelper.getColorName(redColor, greenColor, blueColor);
tvColorName.setText(colorName);
tvRGB.setText("(" + String.valueOf(redColor) + "%, " + String.valueOf(greenColor) + "%, " + String.valueOf(blueColor) + "% )");
}
public void updateColorRGB(int red, int green, int blue) {
String colorHex = String.format("#%02X%02X%02X", red, green, blue);
tvHexCode.setText(colorHex);
ivColorPicked.setBackgroundColor(Color.parseColor(colorHex));
}
}