enter image description here 包装测试; import android.graphics.Canvas; import android.graphics.PointF; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.Toast; import com.davemorrissey.labs.subscaleview.ImageSource; import java.util.ArrayList; import java.util.List; import common.PinView;
public class RoomFragment extends Fragment{
PinView imageView;
LinearLayout layoutFooter;
List<PointF> pinList = new ArrayList<PointF>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.room_fragment_layout, container, false);
//footer action bar
layoutFooter = (LinearLayout) rootView.findViewById(R.id.footer_room);
layoutFooter.setVisibility(LinearLayout.INVISIBLE);
//End footer action bar
//image plane and pin
imageView = (PinView) rootView.findViewById(R.id.plane);
imageView.setImage(ImageSource.resource(R.drawable.plane));
imageView.setZoomEnabled(false);
pinList.add(new PointF(100f, 1200f));
pinList.add(new PointF(400f, 1200f));
pinList.add(new PointF(1000f, 1200f));
imageView.setPins(pinList);
//imageView.setPin(new PointF(800f, 1200f));
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
layoutFooter.setVisibility(LinearLayout.INVISIBLE);
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(getContext(), motionEvent.getX() + " | " + motionEvent.getY(), Toast.LENGTH_SHORT).show();
layoutFooter.setVisibility(LinearLayout.VISIBLE);
}
imageView.setPin(new PointF(motionEvent.getX(), motionEvent.getY()));
return false;
}
});
//End image plane and pin
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
package common;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.Toast;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import java.util.List;
import product.dm.com.dm.R;
public class PinView extends SubsamplingScaleImageView {
private PointF sPin;
private Bitmap pin;
private List<PointF> pinList;
public PinView(Context context) {
this(context, null);
}
public PinView(Context context, AttributeSet attr) {
super(context, attr);
initialise();
}
public void setPin(PointF sPin) {
this.sPin = sPin;
initialise();
invalidate();
}
public void setPins(List<PointF> pinList){
this.pinList = pinList;
initialise();
}
public PointF getPin() {
return sPin;
}
private void initialise() {
float density = getResources().getDisplayMetrics().densityDpi;
pin = BitmapFactory.decodeResource(this.getResources(), R.drawable.pin_red);
float w = (density/420f) * pin.getWidth();
float h = (density/420f) * pin.getHeight();
pin = Bitmap.createScaledBitmap(pin, (int)w, (int)h, true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isReady()) {
return;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
if (sPin != null && pin != null) {
PointF vPin = sourceToViewCoord(sPin);
float vX = vPin.x - (pin.getWidth()/2);
float vY = vPin.y - pin.getHeight();
canvas.drawBitmap(pin, vX, vY, paint);
}
for(int i=0;i<pinList.size();i++){
Paint paints = new Paint();
paint.setAntiAlias(true);
PointF vPin = sourceToViewCoord(pinList.get(i));
float vX = vPin.x - (pin.getWidth()/2);
float vY = vPin.y - pin.getHeight();
canvas.drawBitmap(pin, vX, vY, paints);
}
}
}