将充气视图保存为PNG而不在屏幕上显示

时间:2018-02-14 03:43:38

标签: java android kotlin android-bitmap

我想制作 卡片生成器 应用。因此,用户选择他们的照片,然后按保存按钮,他们得到生成卡(他们的照片,名称和专业)。我想使用XML布局来生成卡片。所以我只需要setImageBitmapsetText而不需要在画布中手动绘制它。但我无法将其保存为PNG。我已经尝试了我的代码,但我得到了黑色空白..

MainActivity

class MainActivity : AppCompatActivity() {
    lateinit var bitmap: Bitmap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlideApp.with(this)
                .asBitmap()
                .load("https://i.stack.imgur.com/Xlsnf.jpg")
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .placeholder(R.drawable.ic_downloading)
                .error(R.drawable.ic_error)
                .into(object : BitmapImageViewTarget(ivImage) {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        super.onResourceReady(resource, transition)
                        bitmap = resource
                        ivImage.setImageBitmap(resource)
                    }
                })

        btSave.setOnClickListener { saveBitmap2() }
    }

    fun saveBitmap2() {
        val inflated = layoutInflater.inflate(R.layout.layout_image_saved, null)
        inflated.ivProfile.setImageBitmap(bitmap)
        inflated.tvName.setText("Zihad Rizky Edwin Fikri")
        inflated.tvProfession.setText("Android Programmer")
        inflated.layout(0, 0, 700, 700)
        val savedBmp = Bitmap.createBitmap(inflated.width, inflated.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(savedBmp)
        canvas.translate(0F, 0F)
        inflated.draw(canvas)

        val bos = ByteArrayOutputStream()
        savedBmp.compress(Bitmap.CompressFormat.PNG, 100, bos)
        val fos = FileOutputStream(File(Environment.getExternalStorageDirectory(), "zihadtest2.png"))
        fos.write(bos.toByteArray())
        fos.flush()
        fos.close()
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="id.osman.viewtobitmaptopng.MainActivity">

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/muslim_men"/>

    <Button
        android:id="@+id/btSave"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivImage"
        app:layout_constraintVertical_bias="1.0"/>

</android.support.constraint.ConstraintLayout>

layout_image_saved.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constrainLayout"
    android:layout_width="700px"
    android:layout_height="700px">

    <ImageView
        android:id="@+id/ivProfile"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/muslim_men"/>

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="Abdullah Supri"
        android:textColor="#111"
        android:textSize="50px"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tvProfession"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="Asisten Pilot"
        android:textColor="#111"
        android:textSize="30px"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvName"/>
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案