【问题标题】:add Logo on images Android在图像上添加徽标 Android
【发布时间】:2021-02-16 20:10:24
【问题描述】:

我正在开发 Android 应用程序,该应用程序必须一次将框架和徽标放在图像上,我面临的问题是框架来自第一个片段,而徽标来自第二个片段。我在 imageView 和 Logo 上的 Bitmap 图像上设置 Frame。

我面临的问题是,因为我成功地在位图图像上成功添加框架,并且我尝试在位图图像上设置徽标,它删除框架并在位图上设置徽标,反之亦然..

我真正想要的是 Frame 和 Logo 一次在 Bitmap 上设置...

这里,Logo 来自 First Fragment Adapter 到 main Activity via Method..

 holder.iconslogo.setOnClickListener {
        when (charItemlogo.itemsidlogo) {
            1 -> {
               var btmp=  arrayList[0].iconslogo
                (context as MakeStylishActivity).setLogos(btmp)
            }

这里的框架是从框架片段到主活动

  holder.iconsframe.setOnClickListener {

        when (charItemFrame.itemsidframe) {
            1 -> {
               var btmp=  arrayList[0].iconsframe
                (context as MakeStylishActivity).setFrames(btmp)
            }}

这是将徽标和框架设置为位图的主要活动

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_make_stylish)
    val byteArray = intent.getByteArrayExtra("pictures")
    bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)!!
    img_bitmap1.setImageBitmap(bmp)
    stringqrcontent= intent.getStringExtra("qrcontent")

    bottom_nav_viewstyle.setOnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.action_default -> {
                true
            }
            R.id.action_colors -> {
                ShowFColorFragment()
                true
            }
            R.id.action_logos -> {
                ShowLogoFragment()
                true
            }
            R.id.action_frames -> {
                FunctionAddFrames();
                true
            }
            R.id.action_patterns -> {
                true
            }
            else -> false
        }
    }
}

fun setLogos(btmp: Bitmap?) {
    //img_bitmap1.setImageBitmap(btmp)
    PutLogoOnQRBitmaps(btmp, bmp!!)
}

fun setFrames(btmp: Bitmap?) {
   // img_bitmap1.setImageBitmap(btmp)
    //addWhiteBorder(bmp!!,10)
    PutFrameImages(btmp, bmp!!)
}

//Combine Frame Behind QR Code
fun PutFrameImages(frame: Bitmap?, image: Bitmap): Bitmap? {
    var cs: Bitmap? = null
    var rs: Bitmap? = null
    rs = Bitmap.createScaledBitmap(frame!!, image.width, image.height, true)
    cs = Bitmap.createBitmap(rs.width, rs.height, Bitmap.Config.RGB_565)
    val comboImage = Canvas(cs)
    comboImage.drawBitmap(image, 0F, 0F, null)
    comboImage.drawBitmap(rs, 0F, 0F, null)
    if (rs != null) {
        rs.recycle()
        rs = null
    }
   // Runtime.getRuntime().gc()
    img_bitmap1.setImageBitmap(cs!!)
    return cs
}


//Put Logo on QR Code
fun PutLogoOnQRBitmaps(logo: Bitmap?, qrcode: Bitmap): Bitmap? {
    val combined = Bitmap.createBitmap(qrcode.width, qrcode.height, qrcode.config)
    val canvas = Canvas(combined)
    val canvasWidth = canvas.width
    val canvasHeight = canvas.height
    canvas.drawBitmap(qrcode, Matrix(), null)
    val resizeLogo = Bitmap.createScaledBitmap(logo!!, canvasWidth / 5, canvasHeight / 5, true)
    val centreX = (canvasWidth - resizeLogo.width) / 2
    val centreY = (canvasHeight - resizeLogo.height) / 2
    canvas.drawBitmap(resizeLogo, centreX.toFloat(), centreY.toFloat(), null)

    img_bitmap1.setImageBitmap(combined)
    return combined
}}

【问题讨论】:

    标签: android-studio android-fragments android-recyclerview layout bitmap


    【解决方案1】:

    我在这里看到了一些不被认为是一个好主意的东西,但最重要的是 Intent 的大小有一个限制(即非常小),并且不是为传递大量数据而设计的。

    我会做什么

    无论您的架构如何(不使用 ViewModel,或正确分离关注点,以及此处忽略的其他一些 SOLID 原则......),我都不会通过意图传递图像。相反,我会(暂时)将图像保存到文件系统,将“路径”作为字符串传递给下一个活动,并让活动打开文件并从文件系统创建位图。

    这意味着您不再需要担心过度使用图像大小/意图大小,并且您的两个活动会稍微解耦。您现在可以将任何路径传递到那里的位图,并且其他活动将拾取它,无论它来自哪里。

    第二个改进是将所有这些(图像存储、传递、检索等)委托给 ViewModel + UseCase(和/或存储库),在这种情况下,您将进一步解耦您的代码。为此,以及更多,起点将是getting started with Android Jetpack;我建议至少尝试在您的架构中利用 a ViewModel

    您还想在不知何故创建位图时要小心,因为您正在做的事情很容易耗尽内存;你应该take a look at Android's official documentation about handling large bitmaps

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2013-10-05
      相关资源
      最近更新 更多