1

Webページの画像に対して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 でエラーが発生し、null ポインター例外で Android でドラッグを開始できません。

助けを求めている

4

0 に答える 0