1

我想在 android web 视图中对网页中的图像进行拖放操作。

代码是这样的。

WebView webView;
ImageView imageView;
ImageView shadowImageView;

private static final String IMAGEVIEW_TAG = "icon bitmap";
public static final String TAG="ResourceSharing";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browser);
    webView=(WebView) findViewById(R.id.webView);

    imageView=(ImageView) findViewById(R.id.downloadImage);
    webView.setWebViewClient(new WebViewController());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.picasaweb.google.com");
    webView.clearCache(true);
    webView.setOnLongClickListener(this);
    shadowImageView=new ImageView(this);
    shadowImageView.setImageBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.picasa_logo));
    shadowImageView.setTag(IMAGEVIEW_TAG);  


}
public class WebViewController extends WebViewClient {

     @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
}
@Override
public boolean onLongClick(View v) {

    HitTestResult hitTestResult=webView.getHitTestResult();
    Log.i(TAG, "Hit type"+hitTestResult.getType());
    switch (hitTestResult.getType()) {
    case HitTestResult.IMAGE_TYPE:
        Log.i(TAG, "Ïnside image type");
        imageView.setVisibility(View.VISIBLE);
        break;
    case HitTestResult.IMAGE_ANCHOR_TYPE:
        Log.i(TAG, "Ïnside image type");
        imageView.setVisibility(View.VISIBLE);
        break;
    case HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
        Log.i(TAG, "Ïnside image type");
        imageView.setVisibility(View.VISIBLE);
        performDragAndDropOperation();
        break;
    default:
        break;
    }

    return true;
}
@TargetApi(11)
private void performDragAndDropOperation(){
    ClipData.Item item = new ClipData.Item((CharSequence) shadowImageView.getTag());
    ClipData dragData = new ClipData((CharSequence) shadowImageView.getTag(),new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN },item);
    DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(shadowImageView);
    shadowImageView.startDrag(dragData, shadowBuilder, null, 0);
}

 private static class MyDragShadowBuilder extends View.DragShadowBuilder {
        private static Drawable shadow;
            public MyDragShadowBuilder(View v) {
                super(v);
                shadow = new ColorDrawable(Color.LTGRAY);
            }
            @Override
            public void onProvideShadowMetrics (Point size, Point touch){
                int width;
                int height;
                Log.i(TAG, "calling getVIew" +getView());
                width = getView().getWidth() / 2;
                height = getView().getHeight() / 2;
                shadow.setBounds(0, 0, width, height);
                size.set(width, height);
                touch.set(width / 2, height / 2);
                Log.i(TAG, "calling getVIew" +getView());
            }
            @Override
            public void onDrawShadow(Canvas canvas) {
                Log.i(TAG, "calling getVIew" +getView());
                shadow.draw(canvas);
            }
        }

但是,当我运行应用程序并在长按后尝试拖动图像时,logcat 中出现错误,无法在 Android 中启动拖动并出现空指针异常。

寻求帮助

4

0 回答 0