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" />
Related
android how to set custom LinearLayout height half of width Inside herself.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec / 2);
}
i use this code but child's are cut off.
public class CustomLinearLayout extends LinearLayout {
private static final float RATIO = 0.5f;
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthSpec, int heightSpec) {
int customHeight = MeasureSpec.makeMeasureSpec(
(int)(MeasureSpec.getSize(widthSpec) * RATIO), MeasureSpec.EXACTLY);
super.onMeasure(widthSpec, customHeight);
}
}
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 built a very simple custom view that only draw a line.
The view is working when I'm running the code but I see nothing on the preview window.
I find solutions, like use Relative layout / ViewGroup, etc. but I want to extend onlyView class (just because I want to know and learn how to do it right).
customView.java
public class customView extends View {
float lineWidth = 5;
Paint linePaint;
public void setLineWidth(float width) {
lineWidth = width;
}
public float getLineWidth() {
return lineWidth;
}
public customView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttributes(context, attrs);
initPaints();
}
private void initAttributes(Context context, AttributeSet attrs) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.customView, 0, 0);
try {
setLineWidth(typedArray.getDimension(R.styleable.customView_cv_line_width, getLineWidth()));
}
finally {
typedArray.recycle();
}
}
private void initPaints() {
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(Color.BLUE);
linePaint.setStrokeWidth(lineWidth);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float midHeight = canvas.getHeight() / 2;
canvas.drawLine(0, midHeight, canvas.getWidth(), midHeight, linePaint);
}
/*// try to operate onMeasure, not helps
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(200, 100);
}*/
}
attrs.xml
<resources>
<declare-styleable name="customView">
<attr name="cv_line_width" format="dimension" />
</declare-styleable>
</resources>
Why do I see an empty rectangle on the XML preview and how can I fix it?
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"));
}
}
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);
}