机器人 - 如何让里面的ImageView图像边缘X / ​​Y位置机器人、图像、边缘、里面

2023-09-05 06:04:35 作者:爱我妥不妥@

喜 如果我有一个充满屏幕的ImageView的。照片 ImageView的背景设置为绿色。 我把一个位图中的ImageView,保持位图的比例。

一个人像照片在这种布局会显示绿色的左侧和右侧 (手机方向=纵向)。

现在,我如何获得的边缘左侧的X / Y位置时绿灯结束,位图开始。

的背景,这种努力的项目是我想写的图像上的文字和背部保存图像与文字的新形象。问题是...

由于我缩放图像inSampleSize = 4;和ImageView的萎缩甚至更多,保存此新的图片会给出一个小小的约250x350的图像。

我要的是使用X / Y位置和书面文本传输到原来的inSampleSize = 4图像或将SD卡1500x3000图像

我知道和了解这个其他问题,我必须做数学计算自己 我只需要这个小的回答。

我忘了,我可以采取截图澄清。

这是什么样子(我得到$ P $一支新钢笔pssing按钮笔每支笔按住屏幕上的自己独特的文字和位置

下面是ImageView的

 进口的java.util.HashMap;
    进口java.util.UUID中;
    进口android.app.Activity;
    进口android.content.Context;
    进口android.graphics.Bitmap;
    进口android.graphics.BitmapFactory;
    进口android.graphics.Canvas;
    进口android.graphics.Color;
    进口android.graphics.Paint;
    进口android.graphics.Point;
    进口android.graphics.Rect;
    进口android.graphics.Typeface;
    进口android.graphics.drawable.BitmapDrawable;
    进口android.graphics.drawable.Drawable;
    进口android.os.Environment;
    进口android.util.AttributeSet;
    进口android.view.Display;
    进口android.view.MotionEvent;
    进口android.widget.EditText;
    进口android.widget.ImageView;

    公共类DrawView2扩展ImageView的{

        私人的HashMap<字符串,ColorBall2> HashBall;
        私人字符串balID =; //变量知道球被拖动
        公共最后弦乐PTPPSERVICE_DERECTORY =/ PTPPservice /;
        私人位图位图;
        私人的EditText编辑;
        民营涂料粉刷=新的油漆();
        民营涂料paint2 =新的油漆();
        民营涂料pTouch =新的油漆();
        私人的EditText addtext;
        私人上下文CTX;
        私人字符串的ImagePath;
        私人布尔removeBall = FALSE;
      INT viewWidth = 0;
        INT viewHeight = 0;
        双bitmapHight = 0;
        双bitmapWidth = 0;
        双比率= 0;

        双STARTX = 0;
        双endX = 0;
        双startY = 0;
        双恩迪= 0;

        公共DrawView2(上下文的背景下,AttributeSet中的ATT,弦乐此搜索){

            超(背景下,的ATT);
            this.ctx =背景;
            this.imagePath =此搜索;
            setFocusable(真正的);

            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setColor(Color.BLACK);
            paint.setTypeface(Typeface.DEFAULT_BOLD);

            paint2.setStyle(Paint.Style.FILL_AND_STROKE);
            paint2.setColor(Color.RED);

          addtext =(EditText上)((活动)CTX).findViewById(R.id.edittextaddtext);

            字符串文件路径= Environment.getExternalStorageDirectory()的toString()+的ImagePath。
            BitmapFactory.Options选项=新BitmapFactory.Options();
            options.inSampleSize = 4;

            位= BitmapFactory.de codeFILE(文件路径,期权);
            //保存比
            INT X = bitmap.getWidth();
            INT Y = bitmap.getHeight();
            如果(Y> X)
                比=((双)Y)/ X;
            如果(X> Y)
                比=((双)X)/年;
            如果(Y == X)
                比= 1;
            可绘制位=新BitmapDrawable(位);
            setImageDrawable(位);

        }
        公共双getRatio(){
            回报率;
        }
        公众的HashMap<字符串,ColorBall2> getHashBall(){
            返回HashBall;
        }
        //返回屏幕上的调整大小的位图
        公共双getOnScreenBitmapHight(){

            返回bitmapHight;
        }
        公共双getOnScreenBitmapWidth(){

            返回bitmapWidth;
        }
        //位图尺寸
        公众诠释getBitmapHight(){

            返回bitmap.getHeight();
        }
        公众诠释getBitmapWidth(){

            返回bitmap.getWidth();
        }
        //获取ImageView的HIGHT宽度
        公众诠释getViewWidth(){
            返回viewWidth;
        }
        公众诠释getViewHeight(){
            返回viewHeight;
        }
        // START END X Y
        公共双getStartX(){
            返回STARTX;
        }
        公共双getEndX(){
            返回endX;
        }
        公共双getStartY(){
            返回startY;
        }
        公共双getEndY(){
            返回恩迪;
        }
        //设置球TEXT
        公共无效addTextToBall(字符串文本){
            如果(balID!=)
            HashBall.get(balID).setText(文本);
        }
        // 路径
        公共字符串getImagePath(){
            返回的ImagePath;
        }
        // THE ORIGINAL INSAMPELSIZE = 4位图
        公共位图getBitmap(){
            返回的位图;
        }
        //停止DRAWAING THE BALL
        公共无效removeBall(布尔值){
           removeBall =价值;
        }
        //返回错误值的RECT
        公共矩形getRect(){

            矩形R = getDrawable()copyBounds()。

            INT drawLeft = r.left;
            INT drawTop = r.top;
            INT drawRight = r.right;
            INT drawBottom = r.bottom;
            返回ř;
        }

        @覆盖
        保护无效onSizeChanged(INT xNew,诠释yNew,诠释xOld,诠释yOld){
            super.onSizeChanged(xNew,yNew,xOld,yOld);
            viewWidth = xNew;
            viewHeight = yNew;
        }

        公共无效addBall(){

//这里我尝试计算边界左,右,上和位图底边
//不要往那个好
            如果(HashBall == NULL)
                HashBall =新的HashMap<字符串,ColorBall2>();

            // X
            双drawAbleWidth = viewWidth /比率;
            STARTX =(viewWidth-drawAbleWidth)/ 2;

            双drawAbleHight = viewHeight /比率;
            startY = drawAbleHight / 2;

            INT BALLY =(viewHeight / 2);
            INT ballX =(viewWidth / 2);

            点1点=新的点();
            point1.x =(INT)ballX;
            point1.y =(INT)BALLY;
            字符串UUID = UUID.randomUUID()的toString()。
            HashBall.put(UUID,(新ColorBall2(CTX,R.drawable.pen1,点1,UUID)));


        }

        @覆盖
        保护无效的OnDraw(帆布油画){
            super.onDraw(画布);


            //canvas.drawCircle(10,10,10,null);
            如果(removeBall&安培;!&安培;!HashBall = NULL){
                对于(字符串键:HashBall.keySet()){
                    //System.out.println("Key:+键+,值:+ map.get(键));
                    如果(addtext!= NULL)
                        //canvas.drawCircle(HashBall.get(键).getX(),HashBall.get(键).getY(),10,paint2);
                        canvas.drawBitmap(HashBall.get(键).getBitmap(),HashBall.get(键).getX() -  10,HashBall.get(键).getY() -  80,NULL);
                      canvas.drawText(HashBall.get(键).getText()+X =+ HashBall.get(键).getX()+Y =+ HashBall.get(键).getY()
                                ,HashBall.get(键).getX(),HashBall.get(键).getY(),油漆);
                }

            }

        }


        //事件触摸屏幕时,
        @覆盖
        公共布尔的onTouchEvent(MotionEvent事件){
            INT eventaction = event.getAction();


            INT X =(int)的event.getX();
            INT Y =(INT)event.getY();

            开关(eventaction)
            {

            案例MotionEvent.ACTION_DOWN://摸下来,检查手指上的球
                balID =;
                对于(字符串键:HashBall.keySet()){

                    //检查球的范围内(圆)
                    //获取中心球
                    INT的centerX = HashBall.get(键).getX()+ 15;
                    INT centerY = HashBall.get(键).getY()+ 15;

                    //计算从触摸半径的球的中心
                    双radCircle =的Math.sqrt((((的centerX-X)*(的centerX-X))+(centerY-Y)*(centerY-Y)));

                    //如果半径越小则23(球的半径为22),那么它必须是在球
                    如果(radCircleγ-33){
                        balID = HashBall.get(键).getID();
                        addtext.setText(HashBall.get(键).getText());
                        打破;
                    }
                }

                打破;


            案例MotionEvent.ACTION_MOVE://触摸拖动球
                //移动球一样的手指
                如果(balID!=){
                    HashBall.get(balID).setX(X-25);
                    HashBall.get(balID).setY(Y-25);

                }
                打破;

            案例MotionEvent.ACTION_UP:
                //触摸滴 - 滴后,只是做的事情在这里

                打破;
            }
            //重绘帆布
            无效();
            返回true;
        }
    }
 

解决方案

如果你知道的比你可以导出将被放置到图像的侧边缘的宽度。

  //这些保存了ImageView的和位图的比例
双bitmapRatio =((双)bitmap.getWidth())/ bitmap.getHeight()
双imageViewRatio =((双)imageView.getWidth())/ imageView.getHeight()
 

现在,如果bitmapRatio比imageViewRatio较大,要知道,这意味着位图比imageview的更宽,如果他们具有相等的高度。换句话说,你必须在顶部和放大器的空白;底部。

相反,如果bitmapRatio比imageViewRatio小那么你就必须要空格左右。从这个你可以得到的坐标pretty的1平凡的,因为它会是0!

 如果(bitmapRatio> imageViewRatio)
{
  drawLeft = 0;
}
其他
{
  drawTop = 0;
}
 

要得到其他统筹,思考你留下和放空间第二种情况;对。这里的位图和ImageView的的高度是相等的,因而在宽度之间的比率等于所述比率之间的比率。你可以用它来计算出宽度的位图,你知道的ImageView的宽度。类似地,可以计算出的高度,如果宽度相等,所不同的是必须使用的比之间的比的倒数作为宽度成反比的比例为:

 如果(bitmapRatio> imageViewRatio)
{
  drawLeft = 0;
  drawHeight =(imageViewRatio / bitmapRatio)* imageView.getHeight();
}
其他
{
  drawTop = 0;
  drawWidth =(bitmapRatio / imageViewRatio)* imageView.getWidth();
}
 

一旦你有了位图的宽度或高度,让空间在一旁很简单,那只是一半的位图和ImageView的宽度或高度的区别:

 如果(bitmapRatio> imageViewRatio)
{
  drawLeft = 0;
  drawHeight =(imageViewRatio / bitmapRatio)* imageView.getHeight();
  drawTop =(imageView.getHeight() -  drawHeight)/ 2;
}
其他
{
  drawTop = 0;
  drawWidth =(bitmapRatio / imageViewRatio)* imageView.getWidth();
  drawLeft =(imageView.getWidth() -  drawWidth)/ 2;
}
 

hi If i have an ImageView that fills the screen. The ImageView background is set to green color. I place a bitmap in the ImageView, keeping bitmap proportions.

A portrait photo in this layout will show green on both left and right side (Phone orientation= portrait).

Now, How do i get the left side x/y position of the edge when the green ends and bitmap begins.

The background for this endeavor project is that i want to write text on the image and save the image back to a new image with the text. The problem is..

Since I scale the image inSampleSize = 4; and the ImageView shrinking it even more, saving this new picture will give a small small approx 250x350 image.

What i want is to use the x/y positions and transfer the written text to the original inSampleSize = 4 image or to the sdcard 1500x3000 image

I know and read other questions about this that i have to "Do the math calculations" myself I just need this small answer.

I forgot i can take a screenshot to clarify.

this is what it look like: (I get a new pen on pressing the button "pen" each pen hold its own unique text and position on the screen

Here is the imageview

    import java.util.HashMap;
    import java.util.UUID;
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.Rect;
    import android.graphics.Typeface;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Environment;
    import android.util.AttributeSet;
    import android.view.Display;
    import android.view.MotionEvent;
    import android.widget.EditText;
    import android.widget.ImageView;

    public class DrawView2 extends ImageView {

        private HashMap<String, ColorBall2> HashBall ;
        private String balID = ""; // variable to know what ball is being dragged
        public final String PTPPSERVICE_DERECTORY = "/PTPPservice/";    
        private Bitmap bitmap;
        private EditText ed;
        private Paint paint = new Paint();
        private Paint paint2 = new Paint();
        private Paint pTouch = new Paint();
        private EditText addtext;
        private Context ctx;
        private String imagePath;
        private boolean removeBall = false;
      int viewWidth = 0;
        int viewHeight = 0;
        double bitmapHight =0;
        double bitmapWidth =0;  
        double ratio =0;

        double startX = 0;
        double endX= 0;
        double startY= 0;
        double endY = 0;

        public DrawView2(Context context, AttributeSet atts,String image1) {

            super(context, atts);
            this.ctx = context;
            this.imagePath = image1;
            setFocusable(true);

            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setColor(Color.BLACK);
            paint.setTypeface(Typeface.DEFAULT_BOLD); 

            paint2.setStyle(Paint.Style.FILL_AND_STROKE);
            paint2.setColor(Color.RED);

          addtext = (EditText) ((Activity) ctx).findViewById(R.id.edittextaddtext); 

            String filePath = Environment.getExternalStorageDirectory().toString() + imagePath;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;

            bitmap = BitmapFactory.decodeFile(filePath,options);
            // SAVE RATIO
            int x = bitmap.getWidth();
            int y = bitmap.getHeight();
            if(y>x)
                ratio = ((double)y)/x;
            if(x>y)
                ratio = ((double)x)/y;  
            if(y==x)
                ratio = 1;
            Drawable bit = new BitmapDrawable(bitmap);
            setImageDrawable(bit);

        }
        public double getRatio() {
            return ratio;
        }
        public HashMap<String, ColorBall2> getHashBall() {
            return HashBall;
        }
        // RETURN THE ON SCREEN RESIZED BITMAP
        public double getOnScreenBitmapHight(){

            return bitmapHight;
        }
        public double getOnScreenBitmapWidth(){

            return  bitmapWidth;
        }
        // BITMAP SIZE
        public int getBitmapHight(){

            return bitmap.getHeight();
        }
        public int getBitmapWidth(){

            return  bitmap.getWidth();
        }
        // GET IMAGEVIEW HIGHT WIDTH
        public int getViewWidth() {
            return viewWidth;
        }
        public int getViewHeight() {
            return viewHeight;
        }
        // START END X Y
        public double getStartX() {
            return startX;
        }
        public double getEndX() {
            return endX;
        }
        public double getStartY() {
            return startY;
        }
        public double getEndY() {
            return endY;
        }
        // SET BALL TEXT
        public void addTextToBall(String text) {
            if(balID != "")
            HashBall.get(balID).setText(text);
        }
        // PATH
        public String getImagePath() {
            return imagePath;
        }
        // THE ORIGINAL INSAMPELSIZE=4 BITMAP
        public Bitmap getBitmap() {
            return bitmap;
        }
        // STOP DRAWAING THE BALL
        public void removeBall(boolean value) {
           removeBall = value;      
        }   
        // THE RECT THAT RETURN WRONG VALUE
        public Rect getRect(){

            Rect r = getDrawable().copyBounds();

            int drawLeft = r.left;
            int drawTop = r.top;
            int drawRight = r.right;
            int drawBottom = r.bottom;
            return r;
        }

        @Override
        protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
            super.onSizeChanged(xNew, yNew, xOld, yOld);
            viewWidth = xNew;
            viewHeight = yNew;
        }

        public void addBall(){

// HERE I TRY TO CALCULATE THE BOUNDS LEFT,RIGHT,TOP AND BOTTOM EDGE OF THE BITMAP
//NOT GOING THAT GOOD
            if(HashBall == null)
                HashBall = new HashMap<String,ColorBall2>();

            //X
            double drawAbleWidth = viewWidth/ratio;
            startX = (viewWidth-drawAbleWidth)/2;

            double drawAbleHight = viewHeight/ratio;
            startY = drawAbleHight/2;

            int ballY = (viewHeight/2); 
            int ballX = (viewWidth/2);

            Point point1 = new Point();
            point1.x = (int) ballX;
            point1.y = (int) ballY;
            String uuId = UUID.randomUUID().toString();
            HashBall.put(uuId,(new ColorBall2(ctx,R.drawable.pen1, point1,uuId)));  


        }

        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);


            //canvas.drawCircle(10,10,10,null);
            if(!removeBall && HashBall != null){
                for (String key : HashBall.keySet()) {
                    //System.out.println("Key: " + key + ", Value: " + map.get(key));
                    if(addtext!=null)
                        //canvas.drawCircle(HashBall.get(key).getX(),      HashBall.get(key).getY(), 10, paint2);
                        canvas.drawBitmap(HashBall.get(key).getBitmap(), HashBall.get(key).getX()-10, HashBall.get(key).getY()-80, null);
                      canvas.drawText  (HashBall.get(key).getText() + "  X="+HashBall.get(key).getX() + "  Y="+HashBall.get(key).getY()
                                , HashBall.get(key).getX(), HashBall.get(key).getY(), paint);
                }

            }

        }


        // events when touching the screen
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int eventaction = event.getAction(); 


            int X = (int)event.getX(); 
            int Y = (int)event.getY(); 

            switch (eventaction ) 
            { 

            case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
                balID = "";
                for (String key : HashBall.keySet()) {

                    // check if inside the bounds of the ball (circle)
                    // get the center for the ball
                    int centerX = HashBall.get(key).getX() + 15;
                    int centerY = HashBall.get(key).getY() + 15;

                    // calculate the radius from the touch to the center of the ball
                    double radCircle  = Math.sqrt( (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));

                    // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
                    if (radCircle < 33){
                        balID = HashBall.get(key).getID();
                        addtext.setText(HashBall.get(key).getText());
                        break;
                    }
                }

                break; 


            case MotionEvent.ACTION_MOVE:   // touch drag with the ball
                // move the balls the same as the finger
                if (balID != "") {
                    HashBall.get(balID).setX(X-25);
                    HashBall.get(balID).setY(Y-25);

                }
                break; 

            case MotionEvent.ACTION_UP: 
                // touch drop - just do things here after dropping

                break; 
            } 
            // redraw the canvas
            invalidate(); 
            return true; 
        }
    }

解决方案

If you know the ratios you can just derive the width of the margin that will be placed to the side of the image.

// These holds the ratios for the ImageView and the bitmap
double bitmapRatio  = ((double)bitmap.getWidth())/bitmap.getHeight()
double imageViewRatio  = ((double)imageView.getWidth())/imageView.getHeight()

Now, if the bitmapRatio is larger than the imageViewRatio, you know that this means that the bitmap is wider than the imageview if they have an equal height. In other words, you'll have blanks on the top & bottom.

Conversely, if bitmapRatio is smaller than imageViewRatio then you'll have blanks to the left and right. From this you can get one of the co-ordinates pretty trivially as it'll be 0!

if(bitmapRatio > imageViewRatio)
{
  drawLeft = 0;
}
else
{
  drawTop = 0;
}

To get the other co-ordinate, think about the second case where you have space left & right. Here the heights of the the bitmap and imageView are equal and thus the ratio between the widths is equal to ratio between the ratios. You can use this to figure out width of the bitmap as you know the width of the imageView. Similarly you can figure out the heights if the width are equal, except that you have to use the inverse of the ratio between the ratios as the width is inversely proportional the the ratio:

if(bitmapRatio > imageViewRatio)
{
  drawLeft = 0;
  drawHeight = (imageViewRatio/bitmapRatio) * imageView.getHeight();
}
else
{
  drawTop = 0;
  drawWidth = (bitmapRatio/imageViewRatio) * imageView.getWidth();
}

Once you have the width or height of the bitmap, getting the space to the side is simple, it is just half the difference between the bitmap and imageView width or height:

if(bitmapRatio > imageViewRatio)
{
  drawLeft = 0;
  drawHeight = (imageViewRatio/bitmapRatio) * imageView.getHeight();
  drawTop = (imageView.getHeight() - drawHeight)/2;
}
else
{
  drawTop = 0;
  drawWidth = (bitmapRatio/imageViewRatio) * imageView.getWidth();
  drawLeft = (imageView.getWidth() - drawWidth)/2;
}