Advertisement
Guest User

Untitled

a guest
May 25th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. //Layout
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8.  
  9. <androidx.constraintlayout.widget.ConstraintLayout
  10. android:id="@+id/parentLayout"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:orientation="vertical">
  14.  
  15. <Button
  16. android:id="@+id/btn_create_image"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginStart="16dp"
  20. android:layout_marginEnd="16dp"
  21. android:text="Create image"
  22. app:layout_constraintEnd_toEndOf="parent"
  23. app:layout_constraintStart_toStartOf="parent"
  24. app:layout_constraintTop_toTopOf="parent" />
  25.  
  26. <ImageView
  27. android:id="@+id/drawingPad"
  28. android:layout_width="match_parent"
  29. android:layout_height="0dp"
  30. android:orientation="vertical"
  31. android:paddingTop="32dp"
  32. android:paddingBottom="32dp"
  33. app:layout_constraintBottom_toBottomOf="parent"
  34. app:layout_constraintTop_toBottomOf="@+id/btn_create_image" />
  35.  
  36. </androidx.constraintlayout.widget.ConstraintLayout>
  37.  
  38. </androidx.core.widget.NestedScrollView>
  39.  
  40.  
  41. //ImageCanvas
  42. public class ImageCanvas extends View {
  43.  
  44. private static final String TAG = "ImageCanvas";
  45.  
  46. private Bitmap icon;
  47.  
  48. public ImageCanvas(Context context) {
  49. super(context);
  50.  
  51. Drawable drawable = ContextCompat.getDrawable(context, R.drawable.main_explore);
  52. if(drawable != null){
  53. icon = ((BitmapDrawable) drawable).getBitmap();
  54. }
  55. Log.d(TAG, "ImageCanvas: " + icon);
  56. }
  57.  
  58. public ImageCanvas(Context context, @Nullable AttributeSet attrs) {
  59. super(context, attrs);
  60. }
  61.  
  62. public ImageCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  63. super(context, attrs, defStyleAttr);
  64. }
  65.  
  66.  
  67. @Override
  68. protected void onDraw(Canvas canvas) {
  69. super.onDraw(canvas);
  70.  
  71.  
  72. canvas.drawBitmap(icon, 0, 0, null);
  73. }
  74. }
  75.  
  76.  
  77. //Fragment
  78. public class NewsFragment extends Fragment {
  79.  
  80. private static final String TAG = "NewsFragment";
  81.  
  82. @BindView(R.id.drawingPad)
  83. ImageView drawingPad;
  84.  
  85. private Context context;
  86.  
  87.  
  88. @Override
  89. public void onCreate(@Nullable Bundle savedInstanceState) {
  90. super.onCreate(savedInstanceState);
  91. context = getContext();
  92.  
  93. Log.d(TAG, "onCreate: Creating new News Fragment");
  94. }
  95.  
  96. @Nullable
  97. @Override
  98. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  99. View view = inflater.inflate(R.layout.fragment_news, container, false);
  100. ButterKnife.bind(this, view);
  101.  
  102. return view;
  103. }
  104.  
  105. @OnClick(R.id.btn_create_image)
  106. void onClickCreateImg() {
  107. Drawable backgroundDrawable = ContextCompat.getDrawable(context, R.drawable.landscape);
  108.  
  109. try {
  110. Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
  111.  
  112. int positionLeft = 0;
  113. int positionTop = 0;
  114. Bitmap mainBitmap = Bitmap.createBitmap(
  115. backgroundBitmap.getWidth(),
  116. backgroundBitmap.getHeight(),
  117. Bitmap.Config.ARGB_8888
  118. );
  119. Canvas canvas = new Canvas(mainBitmap);
  120. canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop, null);
  121.  
  122. drawingPad.setImageBitmap(mainBitmap);
  123. } catch (Exception e) {
  124. Log.e(TAG, "onClickCreateImg: Error: ", e);
  125. }
  126.  
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement