我正在开发一个应用程序。在那里我添加两个视图一个在另一个上面。下面是图像视图 以上是画布视图。显示两个位图,一个在下面的图像视图中,一个位图在上面的画布视图中。我将擦除功能添加到画布视图上方。我想显示当点击一个按钮时,将停止擦除并在上下两个视图中添加缩放功能,当触摸缩放时,它将一次缩放两个视图。我试过这个,但只有一个视图将缩放我怎么能这样做,请帮助我..
class:
public class ImageClass extends Activity{
public static ImageView img,imgbg,i1,i2,i3,i4,i5,i6,save,radius,zoomimg;
int screenWidth, screenHeight;
RelativeLayout relative,rrr,mainrel,relpop;
Panel p;
Button clkk,popupp;
Bitmap bitmap;
int x = 0;
int y =0;
float r =20;
float xend;
float yend;
int pos;
FrameLayout fff;
View v;
AttributeSet attrs;
/*private ArrayList<Path> undonePaths = new ArrayList<Path>();
private ArrayList<Path> paths = new ArrayList<Path>();*/
int slll = Color.BLACK;
public static File mediaFile;
File sdfolder,f;
Canvas pcanvas;
int aa,bb;
public static Bitmap blkwhtbtp,belowbtp,abovebtp;
@SuppressLint({ "CutPasteId", "ShowToast" })
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
Toast.makeText(getApplicationContext(), "Touch to unblur image", Toast.LENGTH_SHORT).show();
relative=(RelativeLayout)findViewById(R.id.rrlayout);
rrr=(RelativeLayout)findViewById(R.id.relativebg);
mainrel=(RelativeLayout)findViewById(R.id.main);
imgbg=(ImageView)findViewById(R.id.imageView1);
p=new Panel(this);
relative.addView(p);
belowbtp=Bitmap.createScaledBitmap(Utils.imageBitmap, screenWidth, screenHeight, true);
imgbg.setImageBitmap(belowbtp);
radiuslayout=new LinearLayout(this);
LinearLayout.LayoutParams radiusparams=new LinearLayout.LayoutParams(screenWidth/2,screenHeight/10);
radiusparams.setMargins(0, 0, 0, 0);
radiuslayout.setLayoutParams(radiusparams);
radiuslayout.setOrientation(LinearLayout.HORIZONTAL);
radius = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
radius.setLayoutParams(pp);
radius.setImageResource(R.drawable.ic_launcher);
radius.setOnClickListener(new OnClickListener() {
@SuppressLint({ "ShowToast", "WrongCall" })
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "radius", 1000).show();
p.mPaint.setXfermode(null);
imgbg.setOnTouchListener(new Touchhimage());
p.detector = new ScaleGestureDetector(getApplicationContext(), p.new ScaleListener());
}
});
class Panel extends View {
private Path mPath;
private Paint mPaint;
//Context context;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
public int selectedcolor;
private static final float MIN_ZOOM = 1f;
private static final float MAX_ZOOM = 5f;
private float scaleFactor = 1.f;
private ScaleGestureDetector detector;
///
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode;
private float startX = 0f;
private float startY = 0f;
private float translateX = 0f;
private float translateY = 0f;
private float previousTranslateX = 0f;
private float previousTranslateY = 0f;
boolean dragged;
MotionEvent ev;
@SuppressLint({ "InlinedApi", "NewApi", "ShowToast" })
@SuppressWarnings("static-access")
public Panel(Context context) {
super(context);
Log.v("Panel", ">>>>>>");
setFocusable(true);
setBackgroundColor(Color.TRANSPARENT);
mPaint = new Paint();
mPaint.setAlpha(0);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPath = new Path();
Bitmap bm=Utils.imageBitmap;
abovebtp=Bitmap.createScaledBitmap(bm, screenWidth, screenHeight, true);
// Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.test);
bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Config.ARGB_8888);
pcanvas = new Canvas();
pcanvas.setBitmap(bitmap);
detector = new ScaleGestureDetector(getContext(), new ScaleListener());
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasWindowFocus);
abovebtp=Bitmap.createScaledBitmap(Utils.imageBitmap, imgbg.getWidth(), imgbg.getHeight(), true);
blkwhtbtp=Blur.fastblur(getApplicationContext(), abovebtp, 24);
pcanvas.drawBitmap(blkwhtbtp, 0, 0, null);
}
protected void onDraw(Canvas canvas) {
// draw a circle that is erasing bitmap
canvas.scale(scaleFactor, scaleFactor);
if((translateX * -1) < 0) {
translateX = 0;
}
else if((translateX * -1) > (scaleFactor - 1) * screenWidth) {
translateX = (1 - scaleFactor) * screenWidth;
}
if(translateY * -1 < 0) {
translateY = 0;
}
else if((translateY * -1) > (scaleFactor - 1) * screenHeight) {
translateY = (1 - scaleFactor) * screenHeight;
}
canvas.translate(translateX / scaleFactor, translateY / scaleFactor);
//pcanvas.drawBitmap(blkwhtbtp, 0, 0, mPaint);
if (bitmap!=null) {
canvas.drawBitmap(bitmap, 0, 0,null);
}
for (Path p : paths){
pcanvas.drawPath(p, mPaint);
}
pcanvas.drawPath(mPath, mPaint);
pcanvas.drawCircle(x, y, r, mPaint);
pcanvas.save();
super.onDraw(canvas);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
// undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
pcanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
paths.add(mPath);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//ev=event;
detector.onTouchEvent(event);
//mScaleDetector.onTouchEvent(event);
x = (int) event.getX();
y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
mode = DRAG;
startX = event.getX() - previousTranslateX;
startY = event.getY() - previousTranslateY;
invalidate();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(getApplicationContext(), "testing", 1000).show();
mode = NONE;
dragged = false;
previousTranslateX = translateX;
previousTranslateY = translateY;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
translateX = event.getX() - startX;
translateY = event.getY() - startY;
double distance = Math.sqrt(Math.pow(event.getX() - (startX + previousTranslateX), 2) +
Math.pow(event.getY() - (startY + previousTranslateY), 2)
);
if(distance > 0) {
dragged = true;
}
invalidate();
break;
case MotionEvent.ACTION_POINTER_UP:
x = (int) event.getX();
y = (int) event.getY();
mPath.lineTo(x, y);
mode = DRAG;
previousTranslateX = translateX;
previousTranslateY = translateY;
invalidate();
break;
}
if ((mode == DRAG && scaleFactor != 1f && dragged) || mode == ZOOM) {
invalidate();
}
return true;
}
public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
invalidate();
return true;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/linear" >
<RelativeLayout
android:id="@+id/relativebg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rrlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
</LinearLayout>