I'm trying to set custom typeface for map markers' cluster icon using SquareTextView, what is a class of android-maps-utils-amap library extending TextView. In DefaultClusterRenderer I'm using this code to set custom typeface but no effect. So please help me to understand what I need to do to change the typeface of SquareTextView
private SquareTextView makeSquareTextView(Context context) {
SquareTextView squareTextView = new SquareTextView(context);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "whatever.ttf");
squareTextView.setTypeface(typeface);
}
And this is the source code of SquareTextView:
package com.amap.api.maps2d.ui;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
public class SquareTextView extends TextView {
private int mOffsetTop = 0;
private int mOffsetLeft = 0;
public SquareTextView(Context context) {
super(context);
}
public SquareTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int dimension = Math.max(width, height);
if (width > height) {
mOffsetTop = width - height;
mOffsetLeft = 0;
} else {
mOffsetTop = 0;
mOffsetLeft = height - width;
}
setMeasuredDimension(dimension, dimension);
}
#Override
public void draw(Canvas canvas) {
canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
super.draw(canvas);
}
}
I have my own implementation for TextView and I use it for setting typeface like the following, and it worked for me:
public class MyNewTextView extends TextView {
private int mOffsetTop = 0;
private int mOffsetLeft = 0;
public MyNewTextView(Context context) {
super(context);
}
public MyNewTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyNewTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setTypeface(Typeface tf, int style) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/whatever.ttf"), style);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int dimension = Math.max(width, height);
if (width > height) {
mOffsetTop = width - height;
mOffsetLeft = 0;
} else {
mOffsetTop = 0;
mOffsetLeft = height - width;
}
setMeasuredDimension(dimension, dimension);
}
#Override
public void draw(Canvas canvas) {
canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
super.draw(canvas);
}
}
So try to override setTypeface() for SquareTextView
Make your own implementation of SquareTextView Try this:
public class MySquareTextView extends SquareTextView {
public MySquareTextView (Context context) {
super(context);
setTypeface();
}
public MySquareTextView (Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface();
}
public MySquareTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeface();
}
public void setTypeface(){
setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Italic.ttf"));
}
}
Related
how can appear pagelines in the editText in android , like picture ..?
It is not possible with Default EditText, you need to find out any custom library to use this type of functionality with EditText.
You can do it with custom EditText like the one below (copied from here).
public class LinedEditText extends EditText {
private Paint mPaint = new Paint();
public LinedEditText(Context context) {
super(context);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint();
}
private void initPaint() {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x80000000);
}
#Override protected void onDraw(Canvas canvas) {
int left = getLeft();
int right = getRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int height = getHeight();
int lineHeight = getLineHeight();
int count = (height-paddingTop-paddingBottom) / lineHeight;
for (int i = 0; i < count; i++) {
int baseline = lineHeight * (i+1) + paddingTop;
canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
}
super.onDraw(canvas);
}
}
I have an application include gif file that extended from a view class. I want to display gif file fill parent means fit on the screen, but my code show it in real size.
This is my code for gif view that use from the Movie:
public class GifView extends View{
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long movieStart;
public GifView(Context context) {
super(context);
init(context);
}
public GifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setFocusable(true);
gifInputStream = context.getResources().openRawResource(R.drawable.m);
gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}
public int getMovieWidth() {
return movieWidth;
}
public int getMovieHeight() {
return movieHeight;
}
public long getMovieDuration() {
return movieDuration;
}
protected void onDraw(Canvas canvas) {
long now = SystemClock.uptimeMillis();
if(movieStart == 0){
movieStart = now;
}
if(gifMovie != null){
int dur = gifMovie.duration();
if(dur == 0){
dur = 1000;
}
int relTime = (int)((now - movieStart)%dur);
gifMovie.setTime(relTime);
gifMovie.draw(canvas, 0, 0);
invalidate();
}
}
}
this is xml's code:
<com.example.test.GifView
android:id="#+id/gefview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I search lots and test this code:
public class SampleView extends View {
private Movie mMovie;
private long mMovieStart;
public SampleView(Context context) {
super(context);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.m);
mMovie = Movie.decodeStream(is);
}
public SampleView(Context context, AttributeSet attrSet) {
super(context, attrSet);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.m);
mMovie = Movie.decodeStream(is);
}
public SampleView(Context context, AttributeSet attrSet, int defStyle) {
super(context, attrSet, defStyle);
setFocusable(true);
java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.mojri);
mMovie = Movie.decodeStream(is);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0x00000000);
Paint p = new Paint();
p.setAntiAlias(true);
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
getHeight() / 2 - mMovie.height() / 2);
invalidate();
}
}
}
and this:
public class Gif_loading_View extends WebView {
public Gif_loading_View(Context context) {
super(context);
// TODO Auto-generated constructor stub
loadUrl("myUrl/m.gif");
}
}
But these views show real size too, and i can't change the size of gif file.
Please help me.
I'm currently using the library TouchImageView here:
https://github.com/MikeOrtiz/TouchImageView
This works perfectly fine when I fill the entire phone's screen with the TouchImageView, but how would I constrain the visible area to a square?
I've tried:
public class SquareTouchImageView extends TouchImageView {
public SquareTouchImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public SquareTouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareTouchImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
But that doesn't let me scroll down to show the rest of the image (if it's taller than it is wide)
Is there a way I can enable a square TouchImageView?
If so how would I be able to do it?
I've solved it by doing the following and I hope this helps whoever else might have this issue in future.
public class SquareTouchImageView extends TouchImageView {
public SquareTouchImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public SquareTouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareTouchImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == 0
|| drawable.getIntrinsicHeight() == 0) {
setMeasuredDimension(0, 0);
return;
}
int drawableWidth = drawable.getIntrinsicWidth();
//int drawableHeight = drawable.getIntrinsicHeight();
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//int heightSize = widthSize;// MeasureSpec.getSize(heightMeasureSpec);
//int heightMode = widthMode;// MeasureSpec.getMode(heightMeasureSpec);
viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
viewHeight = viewWidth;// setViewSize(heightMode, heightSize,
// drawableHeight);
//
// Set view dimensions
//
setMeasuredDimension(viewWidth, viewHeight);
//
// Fit content within view
//
fitImageToView();
}
}
You may have to change some variables in TouchImageView from private to protected.
And XML:
<com.aura.app.widget.SquareTouchImageView
android:id="#+id/scrollingImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
I want to increase height of my custom TextView so I can draw some lines below TextView. Can anyone help me how can I do that? Here is my custom textview.
CustomTextView
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs); init();
}
public CustomTextView(Context context) {
super(context);init();
}
private Paint mStrokeBrush;
private void init(){
mStrokeBrush= new Paint();
mStrokeBrush.setStrokeWidth(5f);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(0f, getHeight());
canvas.drawLine(0, 0, getWidth(), 0, mStrokeBrush);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Edit: I removed a bunch of code which wasn't relevant to your question. Sorry if it caused confusion.
This is a modification of my own code, which adds 20dp to the view's height.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
int viewHeight = MeasureSpec.getSize(heightMeasureSpec);
//
// Set view dimensions. Add 20dp.
//
int dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
setMeasuredDimension(viewWidth, viewHeight + dp);
}
I am trying to make custom Views in android I mention all my arrtrs list in MainActivity and the XML file but I there is an error in main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews.piechart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.customviews.charting.piechart.MainActivity
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
custom:titleText="Background color"
custom:valueColor="#android:color/holo_green_light"
/>
</LinearLayout>
Do you have a constructor with no arguments?
You should add a constructor like:
public MainActivity(Context context, AttributeSet attrs) {
super(context, attrs);
}
or
public MainActivity(Context context) {
super(context);
}
You asked for a simple example of a custom view:
public class BirdView extends ImageView {
private float direction = 0;
Paint paint = new Paint();
public BirdView(Context context) {
super(context);
}
public BirdView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(6);
}
public BirdView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
#Override
public void draw(Canvas canvas) {
canvas.save();
int stratX = getWidth() / 2;
int startY = getHeight() / 2;
canvas.rotate(direction, stratX, startY);
super.draw(canvas);
canvas.restore();
canvas.drawLine(stratX, startY, stratX, startY + 80, paint);
}
public void update(float dir) {
direction = dir;
invalidate();
}
}