SurfaceView shows black screen except icon - java

I used the old guide according to which the icon should appear in surfaceview.
Unfortunately the app dispalys the entire black surfaceview except icon.
I do not
know how to fix this because android studio does not display errors.
What i should add or change?
public class mySurfaceView extends SurfaceView {
private SurfaceHolder surfaceHolder;
private Bitmap bmpIcon;
public mySurfaceView(Context context) {
super(context);
init();
}
public mySurfaceView(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public mySurfaceView(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
surfaceHolder = getHolder();
bmpIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
surfaceHolder.addCallback(new SurfaceHolder.Callback(){
#Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}});
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if (bmpIcon != null){ canvas.drawBitmap(bmpIcon,
getWidth()/2, getHeight()/2, null);
}
}
}
ScreenShot
my icon
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF020000"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>

You gave black color in drawSomething method, If you change the color your background will change.
protected void drawSomething(Canvas canvas) {
//canvas.drawColor(Color.BLACK);
canvas.drawColor(Color.RED); // here i changed black to red(add you expected color here)
if (bmpIcon != null) {
canvas.drawBitmap(bmpIcon,
getWidth() / 2, getHeight() / 2, null);
}
}
As your drawble is vector drawable so use the following function to convert into bitmap
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
and create your bitmap like this
bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);
it will work fine.
and your final code will be like this,
public class MySurface extends SurfaceView {
private SurfaceHolder surfaceHolder;
private Bitmap bmpIcon;
public MySurface(Context context) {
super(context);
init();
}
public MySurface(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public MySurface(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
private void init() {
surfaceHolder = getHolder();
bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(Color.RED);
if (bmpIcon != null) {
canvas.drawBitmap(bmpIcon,
getWidth() / 2, getHeight() / 2, null);
}
}
}

Related

android moving multiple circle on a L shaped path

would try to move multiple points along an L shaped path to simulate water flow from a point to another. I created a view that allow to create a single point and created an animation. Now I'm stuck as I cannot understand how to retrieve values from the animator add points to it and update animation. Can someone try to move me on right path?
public class FlowAnimation extends View implements ValueAnimator.AnimatorUpdateListener {
int radius = 20;
int color = Color.WHITE;
Paint paint = null;
private ValueAnimator mAnimator;
PathMeasure pm;
float point[] = {0f, 0f};
Path path;
public FlowAnimation(Context context) {
super(context);
paint = new Paint();
path = new Path();
path.lineTo(100,150);
path.lineTo(300,500);
path.close();
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
paint.setColor(this.color);
canvas.drawCircle(x / 2, y / 2, this.radius, paint);
}
public void startAnim() {
// sets the range of our value
mAnimator = ValueAnimator.ofInt(0, 180);
// sets the duration of our animation
mAnimator.setDuration(1000);
// registers our AnimatorUpdateListener
mAnimator.addUpdateListener(this);
mAnimator.start();
}
#Override
public void onAnimationUpdate(#NonNull ValueAnimator valueAnimator) {
//gets the current value of our animation
int value = (int) valueAnimator.getAnimatedValue();
}
}

Draw square box using the coordinates on image in android(JAVA)

I have some coordinates which is coming from the backend. I need to draw some rectangles or squares according to the coordinates on image in the (JAVA) android app. The rectangles or squares should be clickable you can see the coordinates in the image
public class DrawView extends View {
Paint paint = new Paint();
private void init() {
paint.setColor(Color.BLACK);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawRoundRect(150,150,300,300,10,10,paint);
canvas.drawRect(300, 300, 600, 600,paint);
}
}

Android ImageView Two Width

Can we apply two widths to ImageView in android?
I searched a lot online for the solution but 1 or 2 solutions I got is via drawable file. But I don't want to do it with drawable because I want to change ImageView width at runtime from seekbar values. There is two seekbar in my application, one will change the width from top and other will change the width from the bottom on a ImageView.
I tried following too:
public class CustomImage extends AppCompatImageView {
private Path path;
public CustomImage(Context context) {
super(context);
init();
}
public CustomImage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
path = new Path();
}
#Override
protected void onDraw(Canvas canvas) {
float h = getMeasuredHeight();
float w = getMeasuredWidth();
path.moveTo(0, 0);
path.lineTo(w, 0);
path.lineTo(w * 0.8f, h);
path.lineTo(w * 0.2f, h);
path.lineTo(0, 0);
path.close();
canvas.clipPath(path);
super.onDraw(canvas);
}
}
But it's not working as I want. No drawable solutions please, because it will not work on my app.

View's invalidate redraws previous texture also

I want to achieve something like this.
but instead of that, I'm getting something like this.
I'm using invalidate to redraw my custom view. But it is creating another view everytime the sides of polygon is changed. Where am I going wrong?
Here is my code.
PolygonView.Java
public class PolygonView extends View {
public float polygonRadius;
public int polygonSides;
public int polygonColor;
private Paint paint;
private Path path;
public PolygonView(Context context) {
super(context);
init(null);
}
public PolygonView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public PolygonView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public PolygonView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(AttributeSet attrs)
{
polygonColor = ContextCompat.getColor(getContext(),R.color.polygonColor);
polygonSides = 5;
if (attrs!=null){
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs,R.styleable.PolygonView,0,0);
polygonColor = typedArray.getColor(R.styleable.PolygonView_polygon_color,polygonColor);
polygonRadius = typedArray.getDimension(R.styleable.PolygonView_polygon_radius,polygonRadius);
polygonSides = typedArray.getInteger(R.styleable.PolygonView_polygon_sides,polygonSides);
}
paint = new Paint();
paint.setColor(polygonColor);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
path = new Path();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
double angle = 2.0*Math.PI/polygonSides;
int cx = getWidth()/2;
int cy = getHeight()/2;
path.moveTo(
(float) (cx +polygonRadius*Math.cos(0.0)),
(float) (cy +polygonRadius*Math.sin(0.0))
);
for(int i=1;i<=polygonSides;i++){
path.lineTo(
(float) (cx + polygonRadius*Math.cos(angle*i)),
(float) (cy + polygonRadius*Math.sin(angle*i))
);
}
path.close();
canvas.drawPath(path,paint);
}
}
Fragment2.Java
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_polygon, container, false);
final PolygonView polygonView = v.findViewById(R.id.polygonView);
SeekBar seekBarRadius = v.findViewById(R.id.seekBarRadius);
SeekBar seekBarSides = v.findViewById(R.id.seekBarSides);
seekBarSides.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int p = progress/10;
if(p<1){
polygonView.polygonSides = 1;
}else if (p>10){
polygonView.polygonSides = 10;
}
else {
polygonView.polygonSides = p;
}
polygonView.invalidate();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return v;
}
}
You are always mutating your path but never reset it. Inside onDraw() method reset the path and only then apply new operations to it:
public void onDraw() {
path.reset();
...
}

Surfaceview to draw Thread error NULLPointerException

I'm a beginner in android. I'm creating a simple surface view to draw a thread (draw rectangle), but it keeps getting error like this :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int)' on a null object reference
and this is my code :
myActivity.java :
public class myActivity extends AppCompatActivity {
mySurfaceView mysurfaceview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball);
//mysurfaceview = new mySurfaceView(this);
mysurfaceview = (mySurfaceView) findViewById(R.id.mySurfaceView);
mysurfaceview.RunThread();
}
mySurfaceview.java :
public class mySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public DrawThread DrawThread;
private Rect myrectangle;
private Paint myPaint;
int moveX = 0;
private void init() {
myrectangle = new Rect(150, 150, 700, 500);
myPaint = new Paint();
myPaint.setColor(Color.BLUE);
}
public mySurfaceView(Context context) {
super(context);
//init();
}
public mySurfaceView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); }
public mySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); }
public void onDrawSomething(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawRect(myrectangle, myPaint);
}
#Override
public void surfaceCreated(SurfaceHolder holder) { }
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
#Override
public void surfaceDestroyed(SurfaceHolder holder) { }
public void update(){
}
public void RunThread() {
init();
DrawThread = new DrawThread(getHolder(), this);
DrawThread.setRunning(true);
DrawThread.start();
}
public class DrawThread extends Thread {
//var definition
public boolean run = false;
private SurfaceHolder surfaceHolder;
mySurfaceView mysurfaceView;
public DrawThread(SurfaceHolder surfaceHolder, mySurfaceView mysurfaceView) {
this.surfaceHolder = surfaceHolder;
this.mysurfaceView = mysurfaceView;
}
public void setRunning(boolean run) {
this.run = run;
}
#Override
public void run() {
while(run)
{
//implement fps counter
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
mysurfaceView.onDrawSomething(canvas);
//mysurfaceView.update();
}
} finally {
if(canvas != null)
{
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
And this is my XML code to show surface view :
<com.example.firstapp_aurelia.mySurfaceView
android:id="#+id/mySurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Did I do anything wrong here? Thank you so much.
I didn't run you code but do not initial mySurfaceView twice
mysurfaceview = new mySurfaceView(this);
mysurfaceview = (mySurfaceView) findViewById(R.id.mySurfaceView);

Categories

Resources