Android-在导入图像中的随机位置上设置Dot

时间:2018-11-28 13:39:40

标签: java android android-studio

提前谢谢!

情况: 我和一个朋友必须为大学开发一个应用程序。 (我认为这个故事很普遍?!)我们想到了一款App的想法,您可以在其中拍摄或导入图片,然后App在该图片上随机地创建一个点/点。 因此,如果您站在商店的货架前,却不知道应该购买哪种啤酒/利口酒/薯片/ ...,请拍照并随机选择点。

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

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);
}

如果我将此代码更改为导入的图片所在的imageview,则在单击“魔术”按钮后图片将变为白色。

我要更改的内容:

  • 设置点(非文本)
  • 在导入的图像中设置点
  • 从图像中获取最大和最小尺寸以设置点(宽度,高度)

我认为在执行此任务时,我犯了一些根本性的错误。但是我不知道... =(

所以希望您能为我提供帮助(正确的代码或提示)

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

2 个答案:

答案 0 :(得分:0)

我很难理解您的操作方式,因为我看不到您的所有代码。但是,如果我会完成此项目,则可以使用FrameLayout,然后添加带有以下内容的照片:

  android:layout_width="match_parent" 
  android:layout_height="match_parent"

然后使用圆形背景做一个小视图,并使左边距右边距为随机。这样一来,您就无需在画布上画画,也无需费劲。

答案 1 :(得分:0)

与此同时,我得到了一种解决方案,可以使图片更清晰。

要将图像设置为imageview:

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

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

现在点击功能:

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;
}

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

相关问题