Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击图像视图在其上绘制画布,在每个触摸事件上垂直移动图像视图。为什么?_Android - Fatal编程技术网

Android 单击图像视图在其上绘制画布,在每个触摸事件上垂直移动图像视图。为什么?

Android 单击图像视图在其上绘制画布,在每个触摸事件上垂直移动图像视图。为什么?,android,Android,我想在图像视图上绘制画布 我正在使用手指画的概念,但它不起作用 public class show_image extends Activity implements OnTouchListener,ColorPickerDialog.OnColorChangedListener { ImageView img_vw_show; Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); sta

我想在图像视图上绘制画布 我正在使用手指画的概念,但它不起作用

public class show_image extends Activity implements OnTouchListener,ColorPickerDialog.OnColorChangedListener {
    ImageView img_vw_show;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    float x1, y1, x2, y2;
    Bitmap bmp;
    int i = 1, width, height;
    private static final float MINP = 0.25f;
    // private static final float MAXP = 0.75f;
    int w, h;
    Bitmap mBitmap, bMap, newBitmap;
    Canvas mCanvas;
    Path mPath;
    Paint mBitmapPaint, mPaint;
    MaskFilter mEmboss;
    MaskFilter mBlur;
    BufferedInputStream buf;
    MyView mv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_image);
        System.out.println("oncreate method called");
        img_vw_show = (ImageView) this.findViewById(R.id.img_vw_show);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

        // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        img_vw_show.setOnTouchListener((OnTouchListener) this);
        String responseImagePath = this.getIntent().getStringExtra(
                "responseImagePath");
        String selectedImagePath = this.getIntent().getStringExtra(
                "selectedImagePath");
        System.out.println("responseImagePath in show image========"
                + responseImagePath);
        System.out.println("selectedImagePath in show image========"
                + selectedImagePath);
        Boolean selectedImagePathFlag = false;

        if (selectedImagePath == null) {
            selectedImagePathFlag = true;

        } else if (selectedImagePath.equals("")) {
            selectedImagePathFlag = true;
        }

        if (selectedImagePathFlag) {
            if (responseImagePath != null && !responseImagePath.equals("")) {
                URL url1;
                InputStream in;

                try {
                    url1 = new
                    // URL("http://mail.sshanghvi.com:8080/sacplupload/checklist/"+responseImagePath);
                    URL("http://192.168.1.49:82/uploads/" + responseImagePath);
                    in = url1.openStream();
                    // Read the inputstream
                    buf = new BufferedInputStream(in);

                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    // The new size we want to scale to
                    final int REQUIRED_SIZE = 70;

                    // Find the correct scale value. It should be the power of
                    // 2.
                    int width_tmp = o.outWidth, height_tmp = o.outHeight;
                    int scale = 1;
                    while (true) {
                        if (width_tmp / 2 < REQUIRED_SIZE
                                || height_tmp / 2 < REQUIRED_SIZE)
                            break;
                        width_tmp /= 2;
                        height_tmp /= 2;
                        scale *= 2;
                    }

                    // Decode with inSampleSize
                    BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    bMap = BitmapFactory.decodeStream(buf, null, o2);
                    img_vw_show.setImageBitmap(bMap);
                    width = bMap.getWidth();
                    height = bMap.getHeight();
                    System.out.println("Width and Height is==" + width + " & "
                            + height);
                    mv = new MyView(show_image.this, bMap);
                    System.out.println("Setting the Myview");
                    mv.invalidate();
                    if (in != null) {
                        in.close();
                    }
                    if (buf != null) {
                        buf.close();
                    }

                } catch (Exception e) {
                    Log.e("Error reading file", e.toString());
                }
            } else {
                Toast.makeText(getApplicationContext(), "Image Not Available",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            File imgFile = new File(selectedImagePath);
            BitmapFactory.Options bfOptions = new BitmapFactory.Options();

            bfOptions.inDither = false; // Disable Dithering mode
            bfOptions.inPurgeable = true; // Tell to gc that whether it needs
                                            // free memory, the Bitmap can be
                                            // cleared
            bfOptions.inInputShareable = true; // Which kind of reference will
                                                // be used to recover the Bitmap
                                                // data after being clear, when
                                                // it will be used in the future
            bfOptions.inTempStorage = new byte[32 * 1024];
            Bitmap myBitmap = BitmapFactory.decodeFile(
                    imgFile.getAbsolutePath(), bfOptions);
            img_vw_show.setImageBitmap(myBitmap);

        }
    }

    public class MyView extends View {

        public MyView(Context c, Bitmap img) {
            super(c);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(8);
            setWillNotDraw(false);
            mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6,
                    3.5f);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }

        Bitmap newBitmap;
        Canvas newCanvas;
        public void onDraw(Canvas canvas) {
            super.onDraw(newCanvas);
            newBitmap= Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            newCanvas= new Canvas(newBitmap);
            System.out.println("canvas == "+canvas);
            System.out.println("newCanvas == "+newCanvas);
            System.out.println("onDraw Called");

            img_vw_show.draw(newCanvas);

            newCanvas.drawBitmap(newBitmap, 0, 0, mBitmapPaint);

            newCanvas.drawPath(mPath, mPaint);

            img_vw_show.setImageBitmap(newBitmap);
        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            System.out.println("Touch Start Called");
            onDraw(newCanvas);
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) {
            System.out.println("Touch Move Called");
            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() {

            System.out.println("Touch Up Called");
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            newCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }
    }
    private static final int COLOR_MENU_ID = Menu.FIRST;
    private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
    private static final int BLUR_MENU_ID = Menu.FIRST + 2;
    private static final int ERASE_MENU_ID = Menu.FIRST + 3;
    private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
        menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
        menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
        menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
        menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');

        /****
         * Is this the mechanism to extend with filter effects? Intent intent =
         * new Intent(null, getIntent().getData());
         * intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
         * menu.addIntentOptions( Menu.ALTERNATIVE, 0, new ComponentName(this,
         * NotesList.class), null, intent, 0, null);
         *****/
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);

        switch (item.getItemId()) {
        case COLOR_MENU_ID:
            new ColorPickerDialog(this, this, mPaint.getColor()).show();
            return true;
        case EMBOSS_MENU_ID:
            if (mPaint.getMaskFilter() != mEmboss) {
                mPaint.setMaskFilter(mEmboss);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case BLUR_MENU_ID:
            if (mPaint.getMaskFilter() != mBlur) {
                mPaint.setMaskFilter(mBlur);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case ERASE_MENU_ID:
            // mPaint.setXfermode(new
            // PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            return true;
        case SRCATOP_MENU_ID:
            // mPaint.setXfermode(new
            // PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
            mPaint.setAlpha(0x80);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void colorChanged(int color) {
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mv.touch_start(x, y);
            v.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            mv.touch_move(x, y);
            v.invalidate();
            break;
        case MotionEvent.ACTION_UP:
            mv.touch_up();
            v.invalidate();
            break;
        }
        return true;
    }

}
public类show\u图像扩展活动在TouchListener、ColorPickerDialog.OnColorChangedListener上实现{
ImageView img_vw_show;
矩阵=新矩阵();
矩阵savedMatrix=新矩阵();
静态最终int NONE=0;
静态最终整数阻力=1;
静态最终整数缩放=2;
int模式=无;
//记住一些关于缩放的事情
PointF start=新的PointF();
PointF mid=新的PointF();
浮动oldDist=1f;
浮球x1、y1、x2、y2;
位图bmp;
int i=1,宽度,高度;
专用静态最终浮动最小值=0.25f;
//专用静态最终浮动最大值=0.75f;
int w,h;
位图mBitmap、bMap、newBitmap;
帆布mCanvas;
路径mPath;
油漆mBitmapPaint,mPaint;
MaskFilter-mEmboss;
MaskFilter-mBlur;
缓冲输入流buf;
MyView mv;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.show_图像);
System.out.println(“调用oncreate方法”);
img_vw_show=(ImageView)this.findViewById(R.id.img_vw_show);
mPath=新路径();
mBitmapPaint=新油漆(油漆抖动标志);
mEmboss=新的浮雕maskfilter(新的float[]{1,1,1},0.4f,6,3.5f);
//mBlur=新的模糊过滤器(8,BlurMaskFilter.Blur.NORMAL);
img_vw_show.setOnTouchListener((OnTouchListener)this);
String responseImagePath=this.getIntent().getStringExtra(
“responseImagePath”);
String selectedImagePath=this.getIntent().getStringExtra(
“selectedImagePath”);
System.out.println(“显示图像中的responseImagePath=
+响应图像路径);
System.out.println(“在显示图像中选择图像路径=
+选择图像路径);
布尔selectedImagePathFlag=false;
如果(selectedImagePath==null){
selectedImagePathFlag=true;
}else if(selectedImagePath.equals(“”){
selectedImagePathFlag=true;
}
如果(选择图像路径标志){
if(responseImagePath!=null&&!responseImagePath.equals(“”){
URL url1;
输入流输入;
试一试{
url1=新
//网址(“http://mail.sshanghvi.com:8080/sacplupload/checklist/“+responseImagePath);
网址(“http://192.168.1.49:82/uploads/“+responseImagePath);
in=url1.openStream();
//读取输入流
buf=新的BufferedInputStream(in);
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
//我们要扩展到的新尺寸
所需的最终int_尺寸=70;
//找到正确的刻度值。它应该是
// 2.
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
bMap=BitmapFactory.decodeStream(buf,null,o2);
img_vw_show.setImageBitmap(bMap);
宽度=bMap.getWidth();
高度=bMap.getHeight();
System.out.println(“宽度和高度=”+宽度+“&”
+高度);
mv=新的MyView(显示图片,这是bMap);
System.out.println(“设置Myview”);
mv.invalidate();
if(in!=null){
in.close();
}
如果(buf!=null){
buf.close();
}
}捕获(例外e){
Log.e(“读取文件时出错”,e.toString());
}
}否则{
Toast.makeText(getApplicationContext(),“图像不可用”,
吐司。长度(短)。show();
}
}否则{
File imgFile=新文件(选择edimagepath);
BitmapFactory.Options bfOptions=新的BitmapFactory.Options();
bfOptions.inDither=false;//禁用抖动模式
bfOptions.inpurgable=true;//告诉gc它是否需要
//可用内存,位图可以
//清除
bfOptions.inInputShareable=true;//将使用哪种类型的引用
//用于恢复位图
//数据被清除后,当
//它将在将来使用
bfOptions.inTempStorage=新字节[32*1024];
位图myBitmap=BitmapFactory.decodeFile(
imgFile.getAbsolutePath(),bfOptions);
img_vw_show.setImageBitmap(myBitmap);
}
}
公共类MyView扩展了视图{
公共MyView(上下文c、位图img){
超级(c);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPai
     Canvas Class:
       public void init() 
 {
    basePaint = new Paint();
    basePaint.setColor(Color.RED);
    basePaint.setStrokeWidth(2);
    basePaint.setStyle(Paint.Style.FILL_AND_STROKE);

    paint = new Paint();
    paint.setStrokeWidth(2);
    paint.setColor(color);


    triPaint = new Paint();
    triPaint.setColor(Color.RED);
    triPaint.setStrokeWidth(2);
    triPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    triPaint1 = new Paint();
    triPaint1.setColor(Color.RED);
    triPaint1.setStrokeWidth(2);
    triPaint1.setStyle(Paint.Style.FILL_AND_STROKE);

    diffPaint = new Paint();
    diffPaint.setColor(color);
    diffPaint.setStrokeWidth(2);
    diffPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    Log.i("color","="+color);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

}

  @Override
   public void onDraw(Canvas canvas) {

    Log.d("check","="+color);
    Log.e("check","after set:"+buf);
    Log.i("new view ", " on draw ");
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(5);
    paint.setColor(Color.WHITE);

    /*basePaint.setStyle(Paint.Style.FILL);
    basePaint.setStrokeWidth(3);
    basePaint.setColor(Color.RED);

    canvas.drawLine(0,40,80,40,basePaint);*/
    path = new Path();
    path.moveTo(0, 30);                             /*For Borders*/
    path.lineTo(30, 0);
    path.lineTo(-30, 0);
    path.close();
    path.offset(20, -5);
    canvas.drawPath(path, paint);

    diffPaint.setStyle(Paint.Style.FILL);
    diffPaint.setStrokeWidth(3);
    diffPaint.setColor(color);                    //To fill The Triangle
    Log.d("Filling","Triangles");

    diffPath = new Path();
    diffPath.moveTo(0, 30);
    Log.d("After","1st line");

    diffPath.lineTo(30, 0);
    Log.d("After","2nd line");

    diffPath.lineTo(-30, 0);
    Log.d("After","3rd line");

    diffPath.close();
    diffPath.offset(20, -5);
    Log.d("Locating","Arrow");

    canvas.drawPath(diffPath, diffPaint);
    Log.d("Drew","Arrow");
    Log.d("Color","="+color);

         triPaint.setStyle(Paint.Style.FILL);
    triPaint.setStrokeWidth(100);
    triPaint.setColor(Color.BLACK);

    triPath = new Path();
    triPath.moveTo(0, -20);
    triPath.lineTo(20, 0);
    triPath.lineTo(-20, 0);                     //To Fill the extra Space
    triPath.close();
    triPath.offset(42,26);
    canvas.drawPath(triPath, triPaint);

    triPaint1.setStyle(Paint.Style.FILL);
    triPaint1.setStrokeWidth(100);
    triPaint1.setColor(Color.BLACK);


    triPath1 = new Path();
    triPath1.moveTo(0, -20);
    triPath1.lineTo(20, 0);
    triPath1.lineTo(-20, 0);
    triPath1.close();
    triPath1.offset(-2,26);
    canvas.drawPath(triPath1, triPaint1);       
}


   UI class:

   public void show(View anchor,int color) {
    preShow();

    int xPos, yPos, arrowPos;

    mDidAction = false; /*Setting the position of arrows drawn using canvas in UI*/

    int[] location = new int[2];

    anchor.getLocationOnScreen(location);

    Rect anchorRect = new Rect(location[0], location[1], location[0]
            + anchor.getWidth(), location[1] + anchor.getHeight());

    mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    int rootHeight = mRootView.getMeasuredHeight();

    if (rootWidth == 0) {
        rootWidth = mRootView.getMeasuredWidth();
    }

    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
    int screenHeight = mWindowManager.getDefaultDisplay().getHeight();

    if ((anchorRect.left + rootWidth) > screenWidth) {
        xPos = anchorRect.left - (rootWidth - anchor.getWidth());
        xPos = (xPos < 0) ? 0 : xPos;

        arrowPos = anchorRect.centerX() - xPos;

    } else {
        if (anchor.getWidth() > rootWidth) {
            xPos = anchorRect.centerX() - (rootWidth / 2);
        } else {
            xPos = anchorRect.left;
        }

        arrowPos = anchorRect.centerX() - xPos;
    }

    int dyTop = anchorRect.top;
    int dyBottom = screenHeight - anchorRect.bottom;

    boolean onTop = (dyTop > dyBottom) ? true : false;

    if (onTop) {
        if (rootHeight > dyTop) {
            yPos = 15;
            LayoutParams l = mScroller.getLayoutParams();
            l.height = dyTop - anchor.getHeight();
        } else {
            yPos = anchorRect.top - rootHeight;
        }
    } else {
        yPos = anchorRect.bottom;

        if (rootHeight > dyBottom) {
            LayoutParams l = mScroller.getLayoutParams();
            l.height = dyBottom;
        }
    }

     showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), arrowPos,color);
             /*updating color of arrow which is the shape drawn in canvas*/

                 container.setBackgroundColor(color);

    setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
    mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);


} 
  Main Activity:

   Whenever the shape needs to be updated by a modification of color:


    btn1 = (Button) this.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            Log.d("In","View");    
            quickAction.show(v,Color.BLUE);

     /*By passing the color the color is getting updated and modified on each button click for the shape drawn using canvas on the imageView*/
        }
    });