首页 > 解决方案 > Android - 在导入图像中的随机位置设置点

问题描述

提前致谢!!!

情况:我和一个朋友要为大学开发一个应用程序。(我认为这个故事很常见?!)我们有一个应用程序的想法,您可以在其中拍摄或导入照片,然后应用程序在这张照片上随机生成一个点/点。因此,如果您站在商店的货架前,不知道应该购买哪种啤酒/白酒/薯片/……,请拍张照片,随机点为您挑选。

问题:我们有一个图像视图,将图片导入到哪里。去画廊或拍照正在工作。但我不知道如何在这个 imageView/picture 中设置一个点。目前我在它上面放了第二个图像视图,其中出现了一个包含“•”的随机文本。它更像是一种解决方法。

MyCanvas 类中点的代码:

int min = 5;
int max = 500;

Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
int i2 = r.nextInt(max - min + 1) + min;

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint pBackground = new Paint();
        pBackground.setColor(Color.TRANSPARENT);
        canvas.drawRect(0, 0, 512, 512, pBackground);
        Paint pText = new Paint();
        pText.setColor(Color.RED);
        pText.setTextSize(40);
        canvas.drawText("•", i1 , i2, pText);
    }

onClick 方法:

public void click_button_magic(View view) {
    View v = new MyCanvas(getApplicationContext());
    Bitmap bitmap =Bitmap.createBitmap(500/*width*/, 500/*height*/, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);
    ImageView iv2 = (ImageView) findViewById(R.id.imageView_point);
    iv2.setImageBitmap(bitmap);
}

如果我将此代码更改为导入图片进入的图像视图,则单击“魔术”按钮后图片会变白。

我想改变的:

我认为对于这项任务,我犯了一些根本性的错误。但我不知道是什么... =(

所以我希望你能帮助我(代码或提示以正确的方式)

非常感谢,我希望我的英语足以理解我的问题!

标签: javaandroidandroid-studio

解决方案


同时,我得到了一个解决方案,可以在图片中找到一个点。

要将图像设置为图像视图:

...
imageview.setImageBitmap(image);
        bm = image;
        iv = imageview;
...

bm 现在是带有所需图像的位图 iv 是图像视图

现在点击功能:

public void click_button_magic(View view) {

    //bei jedem Klick wird das Bild neu geladen, damit bei erneutem Klicken ein neuer Punkt erscheint
    iv.setImageBitmap(bm);

    ImageView img = findViewById(R.id.imageView_photoImport);
    img.setDrawingCacheEnabled(true);
    Bitmap scaledBitmap = img.getDrawingCache();

    //get Maße des IMG
    int targetWidth = scaledBitmap.getWidth();
    int targetHeight = scaledBitmap.getHeight();

    Bitmap targetBitmap =  loadBitmapFromView(iv, iv.getWidth(), iv.getHeight());//Bitmap.createBitmap(targetWidth,targetHeight,Bitmap.Config.ARGB_8888);

    //get random coordinates
    int min = 5;
    int maxWidth = targetWidth;
    int maxHeight = targetHeight;

    Random r = new Random();
    int randomWidth = r.nextInt(maxWidth - min + 1) + min;
    int randomHeight = r.nextInt(maxHeight - min + 1) + min;

    //Paint object
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);

    Canvas canvas = new Canvas(targetBitmap);
    canvas.drawCircle(randomWidth, randomHeight,20, paint);
    img.setImageBitmap(targetBitmap);
}

public static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, width, height);
//Get the view’s background
    Drawable bgDrawable =v.getBackground();
    if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
        bgDrawable.draw(c);
    else
//does not have background drawable, then draw white background on the canvas
        c.drawColor(Color.WHITE);
    v.draw(c);
    return b;
}

我希望它会在未来对某人有所帮助......也许不会,也许会


推荐阅读