SHARE
TWEET

Untitled

a guest May 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.canvasapps;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Path;
  8. import android.graphics.Rect;
  9. import android.support.v4.content.res.ResourcesCompat;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17.     private ImageView mImageView;
  18.     private Canvas mCanvas;
  19.     private Paint mPaint = new Paint();
  20.     private Paint mPaintText = new Paint(Paint.UNDERLINE_TEXT_FLAG);
  21.     private Bitmap mBitmap;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         mImageView = (ImageView)findViewById(R.id.myimageview);
  28.         mPaint.setColor(Color.BLUE);
  29.         mPaintText.setColor(Color.BLACK);
  30.         mPaintText.setTextSize(70);
  31.     }
  32.  
  33.     public void drawSomething(View view) {
  34.         int vWidth = view.getWidth();
  35.         int vHeight = view.getHeight();
  36.         mBitmap = Bitmap.createBitmap(vWidth,vHeight,Bitmap.Config.ARGB_8888);
  37.         mImageView.setImageBitmap(mBitmap);
  38.         mCanvas = new Canvas(mBitmap);
  39.         mCanvas.drawColor(Color.YELLOW);
  40.         mCanvas.drawText("Hello World!",100,100,mPaintText);
  41.  
  42.         mPaint.setColor(Color.RED);
  43.         mPaint.setStrokeWidth(5);
  44.         mCanvas.drawLine(100,200,400,200,mPaint);
  45.  
  46.         mPaint.setColor(Color.WHITE);
  47.         Rect mRect = new Rect();
  48.         mRect.set(100,250,400,400);
  49.         mCanvas.drawRect(mRect,mPaint);
  50.  
  51.         mPaint.setColor(Color.GREEN);
  52.         mCanvas.drawCircle(100,450,200,mPaint);
  53.  
  54.         mPaint.setColor(Color.MAGENTA);
  55.         Path path = new Path();
  56.         path.setFillType(Path.FillType.EVEN_ODD);
  57.         path.moveTo(100,550);
  58.         path.lineTo(400,550);
  59.         path.lineTo(250,700);
  60.         path.close();
  61.         mCanvas.drawPath(path,mPaint);
  62.  
  63.         mPaint.setColor(Color.BLACK);
  64.         Path atap = new Path();
  65.         atap.setFillType(Path.FillType.EVEN_ODD);
  66.         atap.moveTo(400,850);
  67.         atap.lineTo(800,850);
  68.         atap.lineTo(600,600);
  69.         atap.close();
  70.         mCanvas.drawPath(atap,mPaint);
  71.  
  72.         mPaint.setColor(Color.BLACK);
  73.         Rect ruang = new Rect();
  74.         ruang.set(450, 250,750,400);
  75.         mCanvas.drawRect(ruang,mPaint);
  76.  
  77.         view.invalidate();
  78.     }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top