水印的大小因手机而异

时间:2018-06-11 07:12:20

标签: android

我想在我的应用中添加水印。屏幕分为4个矩形,每个的大小都是ScreenWidth * 1 / 4ScreenHeight.And每个矩形包含两个水印。代码发布如下:

final ViewGroup rootView = (ViewGroup) getActivity().findViewById(android.R.id.content);
    ScreenInitControl screenInitControl = new ScreenInitControl(getActivity());
    int screenWidth = screenInitControl.getScreenWidth();
    int screenHeight = screenInitControl.getScreenHeight();
    int bitmapWidth = screenWidth;
    int bitmapHeight = screenHeight / 4;
    Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    bitmap.setHasAlpha(true);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

    // init paint
    Paint paint = new Paint();
    paint.setColor(Color.GRAY);
    paint.setAlpha(100);
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setTextSize(50);

    // the water mark text
    String watermarkText = Config.getIns().getName();

    // draw the first watermark in a row
    int x = 30;
    int y = bitmapHeight / 2;
    Path path = new Path();
    path.moveTo(x, y);
    path.lineTo(bitmapWidth / 2, 30);
    canvas.drawTextOnPath(watermarkText, path, 0, 30, paint);

    // draw the second watermark in a row
    Path path1 = new Path();
    x = bitmapWidth / 2 + 30;
    y = bitmapHeight / 2;
    path1.moveTo(x, y);
    path1.lineTo(bitmapWidth, 30);
    canvas.drawTextOnPath(watermarkText, path1, 0, 30, paint);

    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

    // add the watermark onto background
    final View watermarkView = new View(getActivity());
    watermarkView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, screenHeight));
    if (Build.VERSION.SDK_INT < 16) {
        watermarkView.setBackgroundDrawable(bitmapDrawable);
    } else {
        watermarkView.setBackground(bitmapDrawable);
    }
    rootView.postDelayed(new Runnable() {
        @Override
        public void run() {
            rootView.addView(watermarkView);
        }
    }, 50);

该代码在三星galaxy A5中运行良好。但是当我在OPPO R11S上安装应用程序时,wartermark的尺寸是三星的1/3。我忽略了什么? Samsung screenshot

OPPO screenshot

1 个答案:

答案 0 :(得分:0)

最后,我发现BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap)已过时。我改用BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap),问题解决了。