PDA

View Full Version : دکمه



minajuuuni
سه شنبه 14 آبان 1392, 11:42 صبح
سلام.چطور میشه دکمه را چرخاند؟یعنی اینطوری افقی نباشه زاویه دار بشه.

hamid_hr
سه شنبه 14 آبان 1392, 16:56 عصر
سلام
اول بايد يك كلاس به برنامه خودت اضافه كني به به نام RotateLayout
اين كدهارم بريز توش


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

public class RotateLayout extends ViewGroup {

public static class LayoutParams extends ViewGroup.LayoutParams {

public int angle;

public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateLayout_Layout);
angle = a.getInt(R.styleable.RotateLayout_Layout_layout_an gle, 0);
}

public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
}

}

public RotateLayout(Context context) {
super(context);
}

public RotateLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
}

public View getView() {
return view;
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
view = getChildAt(0);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
if(angle != layoutParams.angle) {
angle = layoutParams.angle;
angleChanged = true;
}

if(Math.abs(angle % 180) == 90) {
measureChild(view, heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(
resolveSize(view.getMeasuredHeight(), widthMeasureSpec),
resolveSize(view.getMeasuredWidth(), heightMeasureSpec));
}
else {
measureChild(view, widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(
resolveSize(view.getMeasuredWidth(), widthMeasureSpec),
resolveSize(view.getMeasuredHeight(), heightMeasureSpec));
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(changed || angleChanged) {
layoutRectF.set(0, 0, r - l, b - t);
layoutTransitionMatrix.setRotate(angle, layoutRectF.centerX(), layoutRectF.centerY());
layoutTransitionMatrix.mapRect(layoutRectFRotated, layoutRectF);
layoutRectFRotated.round(viewRectRotated);
angleChanged = false;
}

view.layout(viewRectRotated.left, viewRectRotated.top, viewRectRotated.right, viewRectRotated.bottom);
}

@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
canvas.rotate(-angle, getWidth() / 2f, getHeight() / 2f);
super.dispatchDraw(canvas);
canvas.restore();
}

@Override
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
invalidate();
return super.invalidateChildInParent(location, dirty);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
touchPoint[0] = event.getX();
touchPoint[1] = event.getY();

layoutTransitionMatrix.mapPoints(childTouchPoint, touchPoint);
event.setLocation(childTouchPoint[0], childTouchPoint[1]);
return super.dispatchTouchEvent(event);
}

@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new RotateLayout.LayoutParams(getContext(), attrs);
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
return layoutParams instanceof RotateLayout.LayoutParams;
}

@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
return new RotateLayout.LayoutParams(layoutParams);
}

private View view;
private int angle;

private final Matrix layoutTransitionMatrix = new Matrix();

private final Rect viewRectRotated = new Rect();

private final RectF layoutRectF = new RectF();
private final RectF layoutRectFRotated = new RectF();

private final float[] touchPoint = new float[2];
private final float[] childTouchPoint = new float[2];

private boolean angleChanged = true;

}


بعد يه xml به قسمت res پوشه values اضافه ميكني به نام attrs.xml
اين كدا رو هم برا اون مينويسي


<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotateLayout_Layout">
<attr name="layout_angle" format="integer" />
</declare-styleable>
</resources>


بعد تو فايل xml خودت ميري و اينطوري دكمه خودتو اضافه ميكني

ابتدا بايد يه rotatelayout بسازي و دكمه رو داخلش بسازي
اين نمونه كد


<hhh.tttt.ddddd.RotateLayout
xmlns:app="http://schemas.android.com/apk/res/hhh.tttt.ddddd"
android:layout_width="50dp"
android:layout_height="50dp" >

<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_angle="45"
android:text="Rotated Button" />
</hhh.tttt.ddddd.RotateLayout>


اول اون hhh.tttt.ddddd.RotateLayout نام پكيجت هست و اون كلاسي كه ساختي
دوم xmlns:app="http://schemas.android.com/apk/res/hhh.tttt.ddddd" رو هم بايد به نام پكيجت عوض كني
سوم app:layout_angle="45" درجه چرخشو مشخص ميكني براش

منبع
http://stackoverflow.com/questions/8047422/is-there-any-way-to-rotate-a-button-without-using-animation-in-android-2-1/8149329#8149329

hamid_hr
پنج شنبه 16 آبان 1392, 11:47 صبح
اينم نمونه
http://hamid_hr.fileup.ir/d/2164910/hamid.zip

ويرايش شد

minajuuuni
پنج شنبه 16 آبان 1392, 14:47 عصر
salam.man nemitunam ino download konam

minajuuuni
پنج شنبه 16 آبان 1392, 16:56 عصر
سلام.مرسی بخاطر برنامتون.اما من می خوام دکمه زاویه داشته باشه اما تو برنامه ای که شما دادین متن دکمه زاویه داره

abbasalim
پنج شنبه 16 آبان 1392, 22:44 عصر
خب میتونید دکمه رو با فوتوشاپ بصورت تصویر بسازید (راست کج زاویه دار هرچی) بعد اون رو بصورت ایمیج باتون وارد کنید و background دکمتون رو = null قرار بدید