Android Convert Px to Dp (Video Aspect Ratio) [duplicate]

Instead of trying to infer the dp conversion factor from the screen’s density classification, you can simply query it directly: getWindowManager().getDefaultDisplay().getMetrics(metrics); float logicalDensity = metrics.density; logicalDensity will then contain the factor you need to multiply dp by to get physical pixel dimensions for the device screen. int px = (int) Math.ceil(dp * logicalDensity);

OpenGL stretched shapes – aspect ratio

glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); float aspect = (float)width / (float)height; glOrtho(-aspect, aspect, -1, 1, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Update: Explanation what happens OpenGL is a state machine and in the case of OpenGL-2.1 and below maintains a set of transformation matrices. A vertex ↑v first gets multiplied with the modelview matrix to yield a … Read more

How do I keep the aspect ratio on image buttons in android?

<LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:id=”@+id/layoutButtons”> <com.package.SquareButton android:layout_height=”fill_parent” android:layout_width=”0dip” android:layout_weight=”1″ <ImageView android:id=”@+id/box1″ android:layout_gravity=”center” android:adjustViewBounds=”true” android:scaleType=”centerInside” android:layout_height=”wrap_content” android:layout_width=”0dp” android:layout_weight=”1″ android:layout_marginLeft=”5dp” android:layout_marginRight=”5dp”/> </com.package.SquareButton> <com.package.SquareButton android:layout_height=”fill_parent” android:layout_width=”0dip” android:layout_weight=”1″ <ImageView android:id=”@+id/box2″ android:layout_gravity=”center” android:adjustViewBounds=”true” android:scaleType=”centerInside” android:layout_height=”fill_parent” android:layout_width=”fill_parent” android:layout_marginLeft=”5dp” android:layout_marginRight=”5dp”/> </com.package.SquareButton> ……… </LinearLayout> And then add this custom button class: public class SquareButton extends LinearLayout { public SquareButton(Context context) { super(context); } public … Read more